Differences between PHP print and print_r functions

 What is the difference between PHP's print and print_r ? These two seemingly similar things are actually completely different things. print itself is not a function. The function is used to output strings, the same as the echo function, while print_r is a function used to output arrays. , The following distinguishes the difference between the two according to the difference in grammar.


Basic syntax of PHP print and print_r functions

print_r (put the array to be output here, bool $return)   print $string;


Explanation of grammatical differences
  • Because print_r itself is a function, it must be written with parentheses to wrap the output array . For detailed usage, please refer to: PHP print_r output array .
  • Print does not need parentheses at all, and outputs the string directly. If you want to use parentheses, you can also refer to the detailed syntax: PHP print output string .
PHP print and print_r function example reference
<?php
 // print example
 $NewString ='Welcome to Wibibi.Have a good day.';
 print $NewString;

 // print_r example
 $Arr = array ('apple','B' => ' banana');
 print_r($Arr); // print the array for the first time
 print_r($Arr, true); // will not print the array
?>
The output of the above example is
Welcome to Wibibi.Have a good day.
Array ( [A] => apple [B] => banana )
The string output in the first line is the result directly output by print , and the array output in the second line is the result output by the print_r function. The print_r function requires attention to the usage of parameters. In versions after PHP 4.3.0, more After the second parameter, we set it to true here, which means that the array will not be output to the web page.

Post a Comment

0 Comments