Display array and object implementation code in php

  • 2020-05-05 11:00:36
  • OfStack

1, print_r ($array/$var)
print means print, and r comes from the word Array, so what this function does is print the contents of an array. It can print both the contents of an array and ordinary variables.
print_r ($_REQUEST) ;
print_r ($_GET); /* print the contents of the form passed using the GET method */
print_r ($_POST); /* print the contents of the array passed using the form POST method */

2, var_dump ($object/$array/$var)
var stands for variables (Variable). Variables include objects, arrays, and scalar variables.
var_dump ($DB); /* print the contents of the $DB database connection object */
var_dump ($fileHandle); /* print the contents of the file handle object */
var_dump ($Smarty); /* print Smarty template object */

3, var_export($object/$array/$var)
outputs or returns the character representation of a variable. This function returns information about the structure of the variable passed to the function, similar to print_r(), except that it returns the representation of the valid PHP code. You can return a representation of the variable by setting the second argument of the function to TRUE.
For example:
 
<?php 
$a = array ( 1,2, array("a","b","c")) ; 
var_export ($a) ; 
echo "<br>" ; 
$v = var_export ( $a , TRUE) ; 
echo $v ; 
?> 

In the above example, $v = var_export ($a, TRUE) means that the source code of PHP is returned, which can be directly used in the array file of PHP script.
Related note:
The above three functions can print the value of the object, the value of the system function and the contents of the array;
△ echo, print, printf can print variable contents, but cannot display arrays and system super variable arrays;
△ print_r and var_dump not only print arrays, scalar variables, but also the contents of objects;
△ var_dump not only prints variables, array contents, but also displays the contents of Boolean variables and resources (Resource).
Delta var_export function returns structural information about the variables passed to the function, similar to the var_dump() function, except that it returns the legal PHP code.

Related articles: