Usage of print_r, var_export, and var_dump in PHP

Source: Internet
Author: User

The article details the differences between print_r, var_export, and var_dump in PHP and their usage in different php applications. If you need them, please refer to them.

We can see that both print_r and var_export can be used as return values. You only need to set the second parameter to true.

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.

 

The Code is as follows: Copy code

<Pre>
<? Php
$ A = array ('A' => 'apple', 'B' => 'bana', 'c' => array ('x', 'y ', 'Z '));
Print_r ($ );
?>
</Pre>

The above code will be output:

The Code is as follows: Copy code

<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

The Code is as follows: Copy code

<? 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.

 

Var_export
(PHP 4> = 4.2.0, PHP 5) var_export-output or return a variable String Representation

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.

You can set the second parameter of the function to TRUE to return the expression of the variable.

Compare var_export () and var_dump ().

 

The Code is as follows: Copy code

<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>


 

Var_dump
(PHP 3> = 3.0.5, PHP 4, PHP 5) var_dump-print variable information

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 the function, and save them to a string type variable.

You can compare var_dump () with print_r ().

 

Example 1. var_dump () Example

The Code is as follows: Copy code

<Pre>
<? Php
$ 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) ""
[1] =>
String (1) "B"
[2] =>
String (1) "c"
}
}*/

 

$ B = 3.1;
$ C = TRUE;
Var_dump ($ B, $ c );

/* Output:
Float (3.1)
Bool (true )*/

?>
</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.