PHP echo, print, Print_r, sprintf, Var_dump, var_expor using the difference _php tips

Source: Internet
Author: User
Tags mixed sprintf

/*******echo********/
echo-output one or more strings
Describe
Echo (String arg1 [, String ...])
Echo () is not actually a function (it is a language structure), so you do not need to use parentheses. Echo () (unlike some other language constructs) is not like a feature, so it cannot always be used in functions. Also, if you want to pass echo () of multiple arguments, the arguments must not be enclosed in parentheses.
Echo () is a command and cannot return a value. Echo can be followed by many parameters, separated by semicolons, such as:
echo $myvar 1;
Echo 1,2, $myvar, "bold";

/*******print********/
print-output one or more strings
Describe
int print (string arg)
Print () is actually not a real function (it is a language structure), so you don't need to use the parentheses of its argument list.
You can return a value with only one argument

/*******print_r () ********/
Print_r
(PHP 4, PHP 5)
print_r– Print Easy to understand information about variables.
description
BOOL Print_r (mixed expression [, bool return])
Note: Parameter return is added during PHP 4.3.0
Print_r () displays information that is easy to understand about a variable. If a string, integer, or float is given, the variable value itself is printed. If you give an array, the keys and elements will be displayed in a certain format. object is similar to an array.
Remember, Print_r () moves the pointer to the last edge of the array. Use Reset () to get the pointer back to the beginning.
<pre>
<?php
$a = Array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => array (' x ', ' y ', ' z '));
Print_r ($a);
?>
</pre>
The code above will output: <pre>
Array
(
[A] => apple
[B] => Banana
[C] => Array
(
[0] => X
[1] => y
[2] => Z
)
)
</pre>
If you want to capture the output of Print_r (), you can use the return parameter. If this parameter is set to True,print_r () The result is not printed (this is the default action), but the output is returned.
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 of the Print_r
?>

Note:If you want to capture the output of Print_r () in a previous version of PHP 4.3.0, you can use the output control function.
Note:In the previous version of PHP 4.0.4, if the given array or object contained a reference directly or indirectly to itself, Print_r () would go on forever. Print_r ($GLOBALS) is an example, because the $GLOBALS itself is a global variable that contains a reference to itself.
/*******sprintf () ********/

/*******var_dump () ********/
Var_dump
(PHP 3 >= 3.0.5, PHP 4, PHP 5)
var_dump-related information about printing variables
description
void Var_dump (mixed expression [, mixed expression [, ...]])
This function displays information about the structure of one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indenting it.
Tip: To prevent programs from outputting results directly to the browser, you can use output control functions to capture the output of this function and save them to a variable of type string.

/*******var_export () ********/
Var_export
(PHP 4 >= 4.2.0, PHP 5)
var_export-output or returns a string representation of a variable
Describe
Mixed var_export (mixed expression [, bool return])
This function returns the structure information about the variable passed to the function, which is similar to Var_dump (), and differs in that it returns a representation that is valid for PHP code.
Var_export must return valid PHP code, that is to say, the code returned by Var_export can be assigned a variable directly as a PHP code. And this variable will get the same type of value as being var_export.
However, when the variable type is resource, it cannot be copied by simple copy, so when the Var_export variable is a resource type, VAR_EXPORT returns null

Copy Code code as follows:

$res = fopen (' status.html ', ' R ');
Var_dump ($res); Resource (2) of type (stream)
Var_export ($res); Null

You can return the representation of a variable by setting the second argument of the function to TRUE.
Copy Code code as follows:

<pre>
<?php
$a = Array (1, 2, Array ("A", "B", "C"));
Var_export ($a);
/* 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.