Differences in debugging var_export print_r and var_dump in PHP

  • 2021-10-24 19:13:21
  • OfStack

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);

Results

(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);

Results

(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);

Results

(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. Detail of output results: var_export ≈ print_r < var_dump

2. When debugging, you don't need to add echo before calling var_export, print_r and var_dump.

3. The second parameter of var_export and print_r is true, and the return value is true. var_dump is not supported, so do not use var_dump when debugging with file_put_contents output.

4. It is recommended to use var_dump directly for debugging the development environment, which can obtain detailed debugging information and code line number positioning; Debugging the production environment uses var_export or print_r, and remember to set the second parameter to true to return the output value instead of directly outputting it to the front-end influence line.

Summarize


Related articles: