Introduction _php Tutorial Print_r, Var_export, var_dump usage in PHP

Source: Internet
Author: User
This article introduces in detail the differences between Print_r, Var_export and Var_dump in PHP, and the usage of these in different applications in PHP, and the friends who need them can refer to

You can see that both Print_r and var_export can be used as return values, just set the second argument to True

Print_r
(PHP 4, PHP 5) print_r– Print easy-to-understand information about variables.

Describe
BOOL Print_r (mixed expression [, bool return])

Note: The parameter return is added when PHP 4.3.0

Print_r () displays easy-to-understand information about a variable. If a string, integer, or float is given, the variable value itself is printed. If an array is given, the keys and elements are displayed in a certain format. object is similar to an array.

Remember that Print_r () moves the pointer of the array to the last edge. Use Reset () to get the pointer back to the beginning.

The code is as follows Copy Code


$a = Array (' a ' = = ' Apple ', ' B ' + = ' banana ', ' c ' = = Array (' x ', ' y ', ' z '));
Print_r ($a);
?>

The above code will output:

The code is as follows Copy Code


Array
(
[A] = Apple
[B] = Banana
[c] = = Array
(
[0] = X
[1] = y
[2] = Z
)
)

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 will not be printed (this is the default action), but its output is returned.

Example 1. Return parameter Example

The code is as follows Copy Code

$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 version prior to PHP 4.3.0, you can use the output control function.

Note: In versions prior to PHP 4.0.4, if the given array or object contains a reference directly or indirectly to itself, Print_r () will continue forever. Print_r ($GLOBALS) is an example, because $GLOBALS itself is a global variable that contains a reference to itself.

Var_export
(PHP 4 >= 4.2.0, PHP 5) var_export– output or return 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, similar to Var_dump (), unlike the PHP code whose returned representation is valid.

You can return the representation of a variable by setting the second argument of the function to TRUE.

Compare Var_export () and Var_dump ().

The code is as follows Copy Code


$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
*/
?>


Var_dump
(PHP 3>= 3.0.5, PHP 4, PHP 5) var_dump– information about printing variables

Describe
void Var_dump (mixed expression [, mixed expression [, ...]])

This function displays structure information about one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indentation.

Tip: To prevent the program from outputting the results directly to the browser, you can use the Output control function (Output-control functions) to capture the output of the function and save it in a string variable.

You can compare Var_dump () with Print_r ().

Example 1. Var_dump () example

The code is as follows Copy Code


$a = Array (1, 2, Array ("A", "B", "C"));
Var_dump ($a); /* Output:
Array (3) {
[0]=>
Int (1)
[1]=>
Int (2)
[2]=>
Array (3) {
[0]=>
String (1) "a"
[1]=>
String (1) "B"
[2]=>
String (1) "C"
}
}*/

$b = 3.1;
$c = TRUE;
Var_dump ($b, $c);

/* Output:
Float (3.1)
BOOL (TRUE) */

?>



http://www.bkjia.com/PHPjc/631298.html www.bkjia.com true http://www.bkjia.com/PHPjc/631298.html techarticle The article introduces in detail about PHP print_r, var_export, var_dump differences and the use of these several different applications in PHP, there is a need for friends can see the PR ...

  • 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: 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.