This article gives you a summary of 7 kinds of PHP commonly used print output functions, respectively, is Echo,print,printf,sprintf,print_r,var_dump,die, I hope to learn PHP can help you
Development will need to debug code, and PHP debugging code can not be like the language of iOS compiler to step through development software such as Xcode, so developers who develop PHP will usually print related information through the output function to debug the code.
The usual printout functions in PHP are explained in one step
Echo
The Echo function can output multiple strings at the same time, with multiple parameters, but does not require the use of parentheses or return values. However, if there is no problem with parentheses, the function will need parentheses:
Echo ' topic.alibabacloud.com<br> '; Echo (' echo can also have brackets <br> ');
Believe that the Echo function is the most used print function!
Print
The print function can only output one string at a time, with only one argument, with parentheses and a return value. Returns flase when its execution fails.
Print (' www.php.cn<br> ');
The print function is also used quite a lot for printing information, but it is not as convenient as ECHO, but it has its usefulness!
Printf
The printf function has two parameters, the first parameter is the specified output format, and the second parameter is the variable to be output. The output format is:
%s: by string;
%d: by integer type;
%b: Press binary;
%x: Press 16 to enter the system;
%o: Press octal;
$f: by floating-point
/* $var = 10;printf (' Integer:%d<br> ', $var);p rintf (' floating-point:%.2f<br> ', $var); Keep two decimal places printf (' String:%s<br> ', $var);p rintf (' binary:%b<br> ', $var);p rintf (' octal:%o<br> ', $var);p rintf (' Hex:%x<br> ', $var); Print Results/* Integer: 10 Float Type: 10.00 string: 102 binary: 10,108 binary: 126 binary: a*/
sprintf
sprintf cannot directly output a variable value, but instead directly reads the value to the specified variable:
$ret = sprintf ('%.2f ', $var); echo "Result: {$ret}<br>";
This function is useful for formatting variable output, and it is used a lot!
Print_r
Print_r This function is used for output arrays, with one or two. If the parameter two is set to Yes, the expression information is not output, but the return is returned directly:
mixed print_r ( mixed $expression [, bool $return = false ] )
$arr = Array (' name ' = ' topic.alibabacloud.com ', ' site ' = ' www.php.cn ');p rint_r ($arr); Echo ' <br> '; Parameter two is set to true to not print, but to return directly $arr1 = Print_r ($arr, true); echo "{$arr 1}<br>";
Var_dump
Var_dump This function is used most in debugging, the content of the output variable, the type, the contents of the string, commonly used in development debugging use:
When printing a string, where there is an object, the print is://String "Var_dump:arrayvar_dump (' Var_dump: '). $arr. ' <br> '); When only the object itself, print the following://"Array (2) {[" Name "]=> string ()" Topic.alibabacloud.com "[" Site "]=> string (19)" www.php.cn "}var_dump ($arr);
Die
Die function is also widely used, during the debugging process, often interrupt the following execution, it will output the content, and then quit the program or not output content:
if (!isset ($type)) {die (' I am die!<br> ');}
Summarize:
The above 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 content, but cannot display arrays and system super variable arrays;
Print_r and Var_dump can not only print arrays, scalar variables, but also print the contents of objects;
The Var_dump statement can not only print variables, array contents, but also display the contents of Boolean variables and resources (resource);
The Var_export function returns the structure information about the variable passed to the function, similar to the Var_dump () function, and the difference is that the content it returns is a valid PHP code.
The above is the entire content of this article, I hope to be able to help you learn.