Differences between several output functions in PHP

Source: Internet
Author: User
What are the differences between several output functions in PHP? Echo: The return value is void. multiple values can be output at a time. multiple values are separated by commas. Echo is a linguistic structure (languageconstruct), that is, a keyword, but not a real function. Therefore, it cannot be used as part of an expression. You do not need to add brackets when using them. Only basic types are supported, except for the Boolean type. when echotrue is used, 1 is displayed. when echofalse is used, what are the differences between several output functions in PHP?

?

Echo:The return value is void. multiple values can be output at a time. multiple values are separated by commas. Echo is a language construct, or keyword. it is not a real function and cannot be used as part of an expression. You do not need to add brackets when using them. Only the basic type is supported, except for the Boolean type. When echo is true, 1 is displayed. When echo is false, nothing is displayed. Echo cannot output the array type;

Print:The return value is int. only one string and one parameter can be output at the same time. parentheses are also required. syntax for separating multiple display variables by commas is not supported. the returned value is flase when execution fails. The usage of print is similar to that of C, so the % of output content will be explained in special. Print can output data of the array type;

Print_r:You can not only print the value of a variable, but also display variable types, and display complex variable types such as arrays and objects. Print_r () moves the array pointer to the last edge. Reset () allows the pointer to return to the beginning. print_r () can print strings and numbers, while arrays are displayed in the form of an enclosed key and a list of values, and starts with Array;

Printf:The source is printf () in c (). This function outputs formatted strings. Syntax: printf ("parameter 1", parameter 2): parameter 1 = what format is output; parameter 2 = output variable. (% S: by string; % d: by integer; % B: By binary; % x: by hexadecimal; % X: by hexadecimal; % o: by octal; % f: by floating point ). After the placeholder is inserted with the % symbol, it consists of numbers and "\ $. F = format: specifies the string and how to format the variables in it. the variables are as follows: printf ("$ % 01.2f", 43.2); // $43.20; $ indicates the filled characters; 0 indicates that the number of digits is not enough to fill in 0 without affecting the original value; 1 indicates the total width of the output; % f indicates a floating point number;

Sprintf:This cannot be output directly. assign a variable first and then output the variable. As follows:

   

?

Var_dump:Void var_dump (mixed expression [, mixed expression [,...]) function: output variable content, type or string content, type, and length. It is often used for debugging. As follows:

  

?

Var_export:Mixed var_export (mixed $ expression [, bool $ return]) returns the structure information about the variable passed to the function, which is similar to var_dump, the difference is that the returned representation is a valid PHP code. You can set the second parameter of the function to TRUE to return the value of the variable. As follows:

  

 

?

Output: array (0 => array (0 => 'A', 1 => 'B', 2 => 'C ',),), 3.1;

Summary:Echo, print, and printf can print the 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 the content of objects.

?

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.