首页 > 代码库 > PHP 其他(非面向对象)语法细节

PHP 其他(非面向对象)语法细节

  1. echo、print、print_r、printf、sprintf、var_dump、var_export的区别
    1. echo
      echo 不是一个函数,而是一个语言构造器,因此可以不使用小括号来指明参数。另外它返回的是void,并不是值,所以不能使用它来赋值。当想给echo传递多个参数时,不能使用小括号(这样会报parse error),应该使用echo ‘ab‘,‘cd‘;的形式(此时的输出为:abcd)。接收的参数类型:string
     1 <?php 2 // You can also use arrays 3 $baz = array("value" => "foo"); 4 echo "this is {$baz[‘value‘]} !"; // this is foo ! 5  6 $foo = "foobar"; 7 $bar = "barbaz"; 8 // If you are not using any other characters, you can just echo variables 9 echo $foo;          // foobar10 echo $foo,$bar;     // foobarbarbaz11 12 // Because echo does not behave like a function, the following code is invalid.13 ($some_var) ? echo ‘true‘ : echo ‘false‘;14 // However, the following examples will work:15 ($some_var) ? print ‘true‘ : print ‘false‘; // print is also a construct, but16                                             // it behaves like a function, so17                                             // it may be used in this context.18 echo $some_var ? ‘true‘: ‘false‘; // changing the statement around19 20 //相对 echo 中拼接字符串而言,传递多个参数比较好,考虑到了 PHP 中连接运算符(“.”)的优先级。 传入多个参数,不需要圆括号保证优先级: 21 echo "Sum: ", 1 + 2;22 echo "Hello ", isset($name) ? $name : "John Doe", "!";23 //如果是拼接的,相对于加号和三目元算符,连接运算符(“.”)具有更高优先级。为了正确性,必须使用圆括号:24 echo ‘Sum: ‘ . (1 + 2);25 echo ‘Hello ‘ . (isset($name) ? $name : ‘John Doe‘) . ‘!‘;
    2. print
      print 也不是函数,也是语言构造器,所以可以不用小括号来指明参数。与echo的区别:1. print只支持一个参数,而echo在不适用小括号时支持参数列表。 2. print有int型的返回值,但是返回的总是1,没什么用处。接收的参数类型:string
     1 <?php 2 ($some_var) ? print ‘true‘ : print ‘false‘;   //具体参见echo用例 3  4 //另外因为print是语言构造器,而不是函数,其园括号不是必须的。在一些使用场景下可能引发问题。例如: 5 //这部分代码来源:http://php.net/manual/zh/function.print.php#85310 6 <?php 7     if (print("foo") && print("bar")) { 8         // 打印出来的是“bar1”     原因不明,没弄懂 9     }10     11     //对于期待的输出,应该这么写:12     if ((print "foo") && (print "bar")) {13         // da打印出来的是“foobar”    14     }
    3. print_r
      bool print_r ( mixed $expression [, bool $return ] )
      print_r() 显示关于一个变量的易于理解的信息。如果给出的是 string、integer 或 float,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素。object 与数组类似。
      如果想捕捉 print_r() 的输出,可使用 return 参数。若此参数设为 TRUE,print_r() 将不打印结果(此为默认动作),而是返回其输出。
     1 <?php 2     $a = array (‘a‘ => ‘apple‘, ‘b‘ => ‘banana‘, ‘c‘ => array (‘x‘,‘y‘,‘z‘)); 3     print_r ($a); 4  /****** 5     打印输出如下 6 Array 7 ( 8     [m] => monkey 9     [foo] => bar10     [x] => Array11         (12             [0] => x13             [1] => y14             [2] => z15         )16 17 )18 ******/19     $results = print_r ($b, true); //$results 包含了 print_r 的输出结果20     var_dump($results);21 /*****22 Array23 (24     [m] => monkey25     [foo] => bar26     [x] => Array27         (28             [0] => x29             [1] => y30             [2] => z31         )32 33 )34 ******/

     

    4. printf
      int printf ( string $format [, mixed $args [, mixed $... ]] )
      
    依据 format 格式参数产生输出。
    5. sprintf
      string sprintf ( string $format [, mixed $args [, mixed $... ]] )
      Returns a string produced according to the formatting string format.
    6. var_dump、var_export
      var_dump   打印变量相关信息(因为只是用于打印,其输出格式更利于阅读,并不是合法的php代码 )
      var_export  输出或返回一个变量的字符串表示. 函数原型:mixed var_export ( mixed $expression [, bool $return ] )    。该函数打印或者返回一个变量,格式是合法的php代码。
     1 <?php 2 $a = array (1, 2, array ("a", "b", "c")); 3 var_export ($a); 4  5 /* 输出: 6 array ( 7   0 => 1, 8   1 => 2, 9   2 => 10   array (11     0 => ‘a‘,12     1 => ‘b‘,13     2 => ‘c‘,14   ),15 )16 */17 18 $b = 3.1;19 $v = var_export($b, TRUE);20 echo $v;21 22 /* 输出:23 3.124 */25 26 class A{27     public $a=1;28     public $b=2;29 }30 31 $tmp = new A;32 var_export($tmp);33 34 class A{35     public $a=1;36     public $b=2;37 }38 39 $tmp = new A;40 var_export($tmp);41 /* 输出42 A::__set_state(array(43    ‘a‘ => 1,44    ‘b‘ => 2,45 ))46 */

     

  2. a
  3. a

PHP 其他(非面向对象)语法细节