当前位置:网站首页>VaR in PHP_ export、print_ r、var_ Differences in dump debugging

VaR in PHP_ export、print_ r、var_ Differences in dump debugging

2022-06-24 12:17:00 PHP Development Engineer

1、output basic type

Code

$n = "test";
var_export($n);
print_r($n);
var_dump($n);
echo '-----------------' . '<br/><br/>';
file_put_contents("index.log", var_export($n, true) . PHP_EOL, FILE_APPEND);
file_put_contents("index.log", print_r($n, true) . PHP_EOL, FILE_APPEND);
file_put_contents("index.log", var_dump($n) . PHP_EOL, FILE_APPEND);

result

(1) front end :

'test'  test  /Users/xjnotxj/Program/PhpstormProject/colin/index.php:9:string 'test' (length=4)  -----------------  /Users/xjnotxj/Program/PhpstormProject/colin/index.php:15:string 'test' (length=4)

(2)index.log:

'test'  test

2、output array

Code

$arr = array(
  "a" => 1,
  "b" => "222",
  "c" => 3,
);
var_export($arr);
print_r($arr);
var_dump($arr);
echo '-----------------' . '<br/><br/>';
file_put_contents("index.log", var_export($arr, true) . PHP_EOL, FILE_APPEND);
file_put_contents("index.log", print_r($arr, true) . PHP_EOL, FILE_APPEND);
file_put_contents("index.log", var_dump($arr) . PHP_EOL, FILE_APPEND);

result

(1) front end :

array ( 'a' => 1, 'b' => '222', 'c' => 3, )  Array ( [a] => 1 [b] => 222 [c] => 3 )  /Users/xjnotxj/Program/PhpstormProject/colin/index.php:13:  array (size=3)  'a' => int 1  'b' => string '222' (length=3)  'c' => int 3  -----------------  /Users/xjnotxj/Program/PhpstormProject/colin/index.php:19:  array (size=3)  'a' => int 1  'b' => string '222' (length=3)  'c' => int 3

(2)index.log:

array ( 'a' => 1, 'b' => '222', 'c' => 3, ) Array ( [a] => 1 [b] => 222 [c] => 3 )

3、output object

Code

class foo
{
  public $n;
  public function do_foo()
  {
    echo "Doing foo." . $this->n;
  }
}
$object = new foo;
var_export($object);
print_r($object);
var_dump($object);
echo '-----------------' . '<br/><br/>';
file_put_contents("index.log", var_export($object, true) . PHP_EOL, FILE_APPEND);
file_put_contents("index.log", print_r($object, true) . PHP_EOL, FILE_APPEND);
file_put_contents("index.log", var_dump($object) . PHP_EOL, FILE_APPEND);

result

(1) front end :

foo::__set_state(array( 'n' => NULL, )) foo Object ( [n] => ) /Users/xjnotxj/Program/PhpstormProject/colin/index.php:19:  object(foo)[1]  public 'n' => null  -----------------  /Users/xjnotxj/Program/PhpstormProject/colin/index.php:25:  object(foo)[1]  public 'n' => null

(2)index.log:

foo::__set_state(array( 'n' => NULL, )) foo Object ( [n] => )

summary :

1、 Details of output results : var_export ≈ print_r < var_dump

2、 When debugging , call var_export、print_r、var_dump When , no need Add before echo .

3、var_export,print_r Of The second parameter is true Return value .var_dump I won't support it , So use file_put_contents Do not use... When output debugging var_dump.

4、 It is recommended that the debugging of the development environment be used directly var_dump, You can get detailed debugging information and code line number positioning ; Debugging of production environment var_export or print_r, Remember to set the second parameter to true Convert to return output value , Not directly output to the front-end influence line .

summary

The above is a brief introduction PHP in var_export、print_r、var_dump The difference in debugging , I hope that's helpful , If you have any questions, please leave me a message , Xiaobian will reply to you in time . Thank you very much for Open source dominates Support for !

Complete example :http://github.crmeb.net/u/defu

come from “ Open source world ” , link :https://ym.baisou.ltd/post/676.html, If you want to reprint , Please indicate the source , Otherwise, the legal liability will be investigated .

原网站

版权声明
本文为[PHP Development Engineer ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210603160936194U.html