Differences between php echo, print, print_r, sprintf, var_dump, and var_expor

Source: Internet
Author: User

/******* Echo ********/
Echo-output one or more strings
Description
Echo (string arg1 [, string...])
Echo () is actually not a function (it is a language structure), so you do not need to use parentheses. Echo () (unlike some other languages) is not like a function, so it cannot always be used in functions. In addition, if you want to pass the echo () of multiple parameters, the parameters must not be enclosed in brackets.
Echo () is a command and cannot return values. Echo can be followed by multiple parameters separated by semicolons, for example:
Echo $ myvar1;
Echo 1, 2, $ myvar, "bold ";

/******** Print ********/
Print-output one or more strings
Description
Int print (string arg)
Print () is actually not a real function (it is a language structure), so you do not need to use the brackets in its parameter list.
Only one parameter can be returned.

/******* Print_r ()********/
Print_r
(PHP 4, PHP 5)
Print_r-prints easy-to-understand information about variables.
Description
Bool print_r (mixed expression [, bool return])
Note: The return parameter is added in PHP 4.3.0.
Print_r () displays easy-to-understand information about a variable. If the value is string, integer, or float, the variable value is printed. If array is provided, keys and elements are displayed in a certain format. Objects are similar to arrays.
Remember, print_r () will move the array pointer to the last edge. Use reset () to bring the pointer back to the beginning.
<Pre>
<? Php
$ A = array ('A' => 'apple', 'B' => 'bana', 'c' => array ('x', 'y ', 'Z '));
Print_r ($ );
?>
</Pre>
The above code will be output: <pre>
Array
(
[A] => apple
[B] => banana
[C] => Array
(
[0] => x
[1] => y
[2] => z
)
)
</Pre>
To capture the output of print_r (), use the return parameter. If this parameter is set to TRUE, print_r () does not print the result (this is the default action), but returns its output.
Example 1. return parameter example
<? Php
$ B = array ('M' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y ', 'Z '));
$ Results = print_r ($ B, true); // $ results contains the output result of print_r
?>

Note:If you want to capture the output of print_r () in a version earlier than PHP 4.3.0, you can use the output control function.
Note:In versions earlier than PHP 4.0.4, if the given array or object contains a reference pointing directly or indirectly to itself, print_r () will continue forever. Print_r ($ GLOBALS) is an example, because $ GLOBALS itself is a global variable and contains references pointing to itself.
/******* Sprintf ()********/

/******* Var_dump ()********/
Var_dump
(PHP 3> = 3.0.5, PHP 4, PHP 5)
Var_dump-prints information about a variable.
Description
Void var_dump (mixed expression [, mixed expression [,...])
This function displays the structure information about one or more expressions, including the expression type and value. The array recursively expands the value and displays its structure through indentation.
Tip: to prevent the program from directly outputting the results to the browser, you can use the output control function to capture the output of this function and save them to a variable of the string type, for example.

/******* Var_export ()********/
Var_export
(PHP 4> = 4.2.0, PHP 5)
Var_export-String Representation of the output or returned variable
Description
Mixed var_export (mixed expression [, bool return])
This function returns the structure information about the variables passed to this function. It is similar to var_dump (). The difference is that the returned representation is legal PHP code.
Var_export must return valid php code, that is, the Code returned by var_export can be directly assigned a variable as a php code. And this variable will get the same type value as var_export.
However, when the variable type is resource, it cannot be simply copied. Therefore, when the var_export variable is resource type, var_export returns NULL.
Copy codeThe Code is as follows:
$ Res = fopen('status.html ', 'R ');
Var_dump ($ res); // resource (2) of type (stream)
Var_export ($ res); // NULL

You can set the second parameter of the function to TRUE to return the expression of the variable.
Copy codeThe Code is as follows:
<Pre>
<? Php
$ A = array (1, 2, array ("a", "B", "c "));
Var_export ($ );
/* Output:
Array (
0 => 1,
1 => 2,
2 =>
Array (
0 => 'A ',
1 => 'B ',
2 => 'C ',
),
)
*/
$ B = 3.1;
$ V = var_export ($ B, TRUE );
Echo $ v;
/* Output:
3.1
*/
?>
</Pre>

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.