PHP output function summary

Source: Internet
Author: User
Tags php print
This article summarizes seven common php print output functions, echo, print, printf, sprintf, print_r, var_dump, die, I hope this article will help you with php learning. I will summarize seven common php print output functions, echo, print, printf, sprintf, print_r, var_dump, die, I hope it will be helpful for you to learn php.

This article has been learning all the functions used to output information in PHP, classifying and comparing them to learn their similarities and differences, and to master the necessary skills in daily development.

Debugging code is required for development. PHP debugging code cannot be used for single-step debugging by using development software such as Xcode in the iOS compiler language, therefore, PHP developers usually print related prompts by outputting functions to debug the code.

The commonly used printing and output functions in PHP are explained step by step.

Echo

The echo function can output multiple strings with multiple parameters at the same time, but it does not require parentheses or return values. However, if there is no problem with parentheses, the function requires parentheses:

Echo's technical blog
'; Echo ('echo can also contain parentheses
');

I believe the echo function is the most used print function!

Print

The print function can output only one string and only one parameter. it must contain parentheses and return values. Flase is returned when execution fails.

print('www.huangyibiao.com
');

The print function is also widely used to print information, but it is not as convenient as echo, but it is useful!

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. Output format:

% S: by string;
% D: integer type;
% B: in binary format;
% X: in hexadecimal format;
% O: by octal;
$ F: Floating point type

/* $ Var = 10; printf ('integer: % d
', $ Var); printf ('floating point: %. 2f
', $ Var); // retain the two decimal places printf (' string: % s
', $ Var); printf ('binary: % B
', $ Var); printf ('octal sequence: % o
', $ Var); printf ('hexadecimal: % x
', $ Var); // print the result/* integer: 10 floating point: 10.00 string: 10 binary: 1010 octal: 12 hexadecimal: */

Sprintf

Sprintf cannot directly output the variable value, but directly reads the value to the specified variable:

$ Ret = sprintf ('%. 2f', $ var); echo "result: {$ ret}
";

This function is very useful for formatting variable output!

Print_r

Print_r is used to output an array with one or two values. If parameter 2 is set to YES, return is returned instead of the expression information:

mixed print_r ( mixed $expression [, bool $return = false ] )

$ Arr = array ('name' => 'Guy's technical blog ', 'site' => 'Www .huangyibiao.com'); print_r ($ arr); echo'
'; // If parameter 2 is set to true, it will not be printed, but $ arr1 = print_r ($ arr, true) will be returned directly; echo "{$ arr1}
";

Var_dump

Var_dump is the most widely used function during debugging. it is used to output the content, type, and string of variables. it is often used for debugging during development:

// When a string is printed with an object, the output is: // string (20) "var_dump: Arrayvar_dump ('Var _ dump: '. $ arr .'
'); // When only the object itself exists, it is printed as follows: // "array (2) {[" name "] => string (21) "'s technical blog" ["site"] => string (19) "www.huangyibiao.com"} var_dump ($ arr );

Die

The die function is also widely used. during debugging, the following execution is often interrupted. it outputs the content first, then exits the program or does not output the content:

if (!isset($type)) { die('I am die!
');}

Summary:

All the above functions can print the object value, system function value, and array content;
Echo, print, and printf can print variable content, but cannot display arrays and system super variable arrays;
Print_r and var_dump can print not only arrays and scalar variables, but also object content;
The var_dump statement not only prints variables and arrays, but also displays Boolean variables and resources;
The var_export function returns the structure information about the variables passed to the function. similar to the var_dump () function, the difference is that the returned content is legal php code.

The above section details the summary of the PHP print output function. For more information, see other related articles in the first PHP community!

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: info-contact@alibabacloud.com 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.