02-PHP使用过的函数 11-20

  11、printf

输出格式化字符串

//格式化操作printf(模板,数据)
//%s字符串,%d数值
 printf('<pre> %s </pre>',print_r($arr,true));
 Array
(
    [0] => red
    [1] => green
    [2] => blue
)

 12、sprintf

输出格式化字符串

 //printf也可以只返回,不打印
 sprintf('<pre> %s </pre>',print_r($arr,true));

 //加echo输出

 echo sprintf('<pre> %s </pre>',print_r($arr,true));
 Array
(
    [0] => red
    [1] => green
    [2] => blue
)

13、vprintf 

输出格式化字符串

示例1: 

$item = ['电脑',8000,1];
 printf('品名: %s, 单价: %d,数量: %d',$item[0],$item[1],$item[2]);

 echo '<hr>';

 //直接用vprintf可以直接接收数组,使用一个数组表示数据列表

/**
 * vprintf和printf都是(模板,数据)的格式
 * 其区别在于:vprintf数据可以是数组,模板可以依次读取,printf不行,必须一个一个写变量
 */

 vprintf('品名: %s, 单价: %d,数量: %d',$item);

 示例2:

foreach($users as $user){
    //$value是一个数组
    // print_r($value);
    vprintf('id= %s,name= %s<br>',$user);
}
/**
 * vprintf和printf都是(模板,数据)的格式
 * 其区别在于:vprintf数据可以是数组,模板可以依次读取,printf不行,必须一个一个写变量
 */
echo '<hr>';
foreach($users as list('id'=>$id,'name'=>$name)){
    //$value是一个数组
    // print_r($value);
    printf('id = %s, name = %s<br>',$id,$name);
}
echo '<hr>';

以上例程的输出类似于:

id= 5,name= Mike
id= 8,name= John
id= 14,name= Jerry


id = 5, name = Mike
id = 8, name = John
id = 14, name = Jerry 

14、vsprintf

输出格式化字符串

  //vprintf也可以只返回,不打印,用vsprintf

vsprintf('品名: %s, 单价: %d,数量: %d',$item);

   //加echo输出

echo vsprintf('品名: %s, 单价: %d,数量: %d',$item);

15、PDO

代表 PHP 和数据库服务之间的一个连接

16、 call_user_func_array

调用回调函数,并把一个数组参数作为回调函数的参数

示例1: 

<?php
function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
    function bar($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2\n";
    }
}


// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));

// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>

 以上例程的输出类似于:

foobar got one and two
foo::bar got three and four

示例2:call_user_func_array()使用命名空间的情况 

<?php

namespace Foobar;

class Foo {
    static public function test($name) {
        print "Hello {$name}!\n";
    }
}

call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));

call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));

?>

 以上例程的输出类似于:

Hello Hannes!
Hello Philip!

示例3:

class Demo1
{
    public function hello(string $name):string
    {
        return 'hello' .$name;
    }
}
$obj = new Demo1;
echo call_user_func_array([$obj,'hello'],['狗老师']) . '<br>';

 call_user_func_array(函数名,参数),当函数在类里面时,先进行类的实例化,然后可以用:[对象名,‘函数名’]来引用该函数

 示例4:把完整的函数作为回调传入call_user_func_array()

<?php

$func = function($arg1, $arg2) {
    return $arg1 * $arg2;
};

var_dump(call_user_func_array($func, array(2, 4)));

?>

以上例程会输出:

int(8)

示例5:传引用

<?php

function mega(&$a){
    $a = 55;
    echo "function mega \$a=$a\n";
}
$bar = 77;
call_user_func_array('mega',array(&$bar));
echo "global \$bar=$bar\n";

?>

 以上例程会输出:

function mega $a=55
global $bar=55 

注意:

想要用地址作为参数,则必须用array(&)引入,不能直接函数名(&) 

17、call_user_func 

把第一个参数作为回调函数调用

示例1: 

<?php
error_reporting(E_ALL);
function increment(&$var)
{
    $var++;
}

$a = 0;
call_user_func('increment', $a);
echo $a."\n";

// it is possible to use this instead
call_user_func_array('increment', array(&$a));
echo $a."\n";

// it is also possible to use a variable function
$increment = 'increment';
$increment($a);
echo $a."\n";
?>

以上例程会输出:

Warning: Parameter 1 to increment() expected to be a reference, value given in …
0
1
2

注意:请注意,传入call_user_func()的参数不能为引用传递。 

示例2:

<?php
function barber($type)
{
    echo "You wanted a $type haircut, no problem\n";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
?>

 以上例程会输出:

You wanted a mushroom haircut, no problem
You wanted a shave haircut, no problem

示例3: call_user_func() 命名空间的使用 

<?php

namespace Foobar;

class Foo {
    static public function test() {
        print "Hello world!\n";
    }
}

call_user_func(__NAMESPACE__ .'\Foo::test');
call_user_func(array(__NAMESPACE__ .'\Foo', 'test'));

?>

 以上例程会输出:

Hello world!
Hello world!

示例4:用call_user_func()来调用一个类里面的方法

<?php

class myclass {
    static function say_hello()
    {
        echo "Hello!\n";
    }
}

$classname = "myclass";

call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello');

$myobject = new myclass();

call_user_func(array($myobject, 'say_hello'));

?>

 以上例程会输出:

Hello!
Hello!
Hello!

示例5:把完整的函数作为回调传入call_user_func()

<?php
call_user_func(function($arg) { print "[$arg]\n"; }, 'test');
?>

以上例程会输出: 

[test]

 18、__construct(构造函数)

//new的时候要传参,参数会传递给构造函数

$obj = new class(15000)
{
  public float $salary;
  //!构造函数要用两个下划线+construct
  public function __construct(float $salary)
  {
    //设置属性初始值
    $this->salary = $salary;
  }
};

19、gettype

获取变量的类型

获取变量的类型

返回 PHP value 变量的类型。 对于类型检查,请使用 is_* 函数。

返回值: 

返回字符串,可能值为:

  • "boolean"
  • "integer"
  • "double" (由于历史原因,如果是浮点型,则返回 "double",而不仅仅是 "float"
  • "string"
  • "array"
  • "object"
  • "resource"
  • "resource (closed)" 自 PHP 7.2.0 起
  • "NULL"
  • "unknown type"

<?php

$data = array(1, 1., NULL, new stdClass, 'foo');

foreach ($data as $value) {
    echo gettype($value), "\n";
}

?>

integer
double
NULL
object
string

20、fopen 

打开文件或者 URL

示例1: 

//资源类型
$f = fopen('demo1.php','r');
echo gettype($f) . '<br>';

 

示例2:

<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password@example.com/somefile.txt", "w");
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七色的天空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值