zoukankan      html  css  js  c++  java
  • 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;          // foobar
      10 echo $foo,$bar;     // foobarbarbaz
      11 
      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, but
      16                                             // it behaves like a function, so
      17                                             // it may be used in this context.
      18 echo $some_var ? 'true': 'false'; // changing the statement around
      19 
      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] => bar
      10     [x] => Array
      11         (
      12             [0] => x
      13             [1] => y
      14             [2] => z
      15         )
      16 
      17 )
      18 ******/
      19     $results = print_r ($b, true); //$results 包含了 print_r 的输出结果
      20     var_dump($results);
      21 /*****
      22 Array
      23 (
      24     [m] => monkey
      25     [foo] => bar
      26     [x] => Array
      27         (
      28             [0] => x
      29             [1] => y
      30             [2] => z
      31         )
      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代码。
        对于var_export 返回的变量的字符串,可以使用eval 将字符串作为PHP代码执行,生成对应的php数据结构。但是eval语言构造器允许执行任何的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 $aa = var_export($a,true);
      18 eval('$aaa='.$aa.';');    //这时,$aaa就是有和$a相同的数组了。
      19 
      20 $b = 3.1;
      21 $v = var_export($b, TRUE);
      22 echo $v;
      23 
      24 /* 输出:
      25 3.1
      26 */
      27 
      28 class A{
      29     public $a=1;
      30     public $b=2;
      31 }
      32 
      33 $tmp = new A;
      34 var_export($tmp);
      35 
      36 class A{
      37     public $a=1;
      38     public $b=2;
      39 }
      40 
      41 $tmp = new A;
      42 var_export($tmp);
      43 /* 输出
      44 A::__set_state(array(
      45    'a' => 1,
      46    'b' => 2,
      47 ))
      48 */
    2. include、require以及include_once、require_once
      a. include、require、include_once、require_once的引入机制:1. 如果定义了路径(绝对路径或者相对路径),按照路径去寻找文件。2. 没有定义路径,在php.ini中的定义的include_path中的路径中寻找。include_path一般是“.:other_path1:other_path2”。
      b. 当一个文件被包含时,其中所包含的代码继承了 include 所在行的变量范围。从该处开始,调用文件在该行处可用的任何变量在被调用的文件中也都可用。不过所有在包含文件中定义的函数和类都具有全局作用域。
      c. 处理返回值。1. 引入失败时,include 返回 FALSE 并且发出警告。2. 引入成功时,且被引入文件没有通过关键字return给出返回值,则返回1。 3. 引入成功,且被引入文件通过return给出了返回值,则返回定义的返回值。
      d. require_once 语句和 require 语句完全相同,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含。include_once同理
      include与require的区别:
      1. 出错处理方式不同。require 在出错时产生 E_COMPILE_ERROR 级别的错误,即将导致脚本中止。而include 只产生警告(E_WARNING),脚本会继续运行。
      其他注明:
      网络上有说"require是无条件包含,也就是如果一个流程里加入require,无论条件成立与否都会先执行require。而include一般是放在流程控制的处理部分中PHP程序网页在读到include的文件时,才将它读进来"。经过测试,这种说法是错误的。
       1 c.php
       2 <?php
       3 echo "执行c.php
      ";
       4 if(FALSE){
       5     require('d.php');
       6 }
       7 if(FALSE){
       8     include('d.php');
       9 }
      10 
      11 
      12 d.php
      13 <?php
      14 echo "in d.php
      ";

       

    3. a
  • 相关阅读:
    设计模式
    jQuery回到顶部插件jQuery GoUp
    CentOS7+Tomcat 生产系统部署
    iOS 时间戳转换为时间
    iOS开发系列--Swift 3.0
    IOS
    iOS之宏定义#define
    #define和预编译指令
    iOS宏定义的使用与规范
    ios十进制、十六进制字符串,byte,data等之间的转换
  • 原文地址:https://www.cnblogs.com/jade640/p/6683513.html
Copyright © 2011-2022 走看看