Summary of various printout functions in PHP

Source: Internet
Author: User
Tags sprintf
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


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 ' technology blog <br> '; Echo (' echo can also have brackets <br> ');

Believe that the Echo function is the most used print function!


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 ('<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!


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 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 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 ' = ' technology blog ', ' site ' = ' ');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 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 (21)" Technical Blog "[" Site "]=> string (" "}var_dump ($arr);


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


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 , you can also print the contents of the object; 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 variables passed to the function, and Var_dump ( function is similar, the difference is that the content returned is a valid PHP code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.