A detailed explanation of the var_dump () function in PHP

Source: Internet
Author: User
Tags mixed

Var_dump ()

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

The Var_dump () method is to determine the type and length of a variable and output the value of the variable if the variable has a value that is the value of the variable and returns the data type.
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.


Example 1

The code is as follows Copy Code

<?php
$a = "Alsdflasdf;a";
$b = Var_dump ($a);
echo "<br>";
Var_dump ($c);
$d =var_dump ($c);
echo "<br>";
echo $a;
echo "<br>";
Echo $b;
echo "<br>";
Output:
String (a) "Alsdflasdf;a"
Null
Alsdflasdf;a

Example 2

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) "a"
[1]=>
String (1) "B"
[2]=>
String (1) "C"
}
}

*/

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

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

*/
?>
</pre>


Var_export and serialize do array caching


$str = serialize ($arr);
The strings after the conversion of these two mechanisms are not the same, the first is the prototype pattern of the array, and the second is the serialized form. The first deposit in the file as long as the addition of <?php?> tag, the form of an available array prototype, for the call, without conversion, directly return to the array can be, but the second, you need to use the unserialize function of the reverse serialization. For the first one, there is one more step. Come down and we'll talk with the data:

The code is as follows Copy Code

?
Set_time_limit (50);
$a = array (1,2,3);
$b = Array (' A ' =>1, ' B ' =>2, ' C ' =>3);
$c = Array (' A ' =>array (1,2,3), ' B ' =>array (4,5,6));


$time 1 = microtime (true);


$times = 1000000; #10w


for ($i =1; $i <= $times; $i + +) {
$A = Var_export ($a, true);
}


$time 2 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$B = Var_export ($b, true);
}


$time 3 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$C = Var_export ($c, true);
}


$time 4 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$X = serialize ($a);
}


$time 5 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$Y = serialize ($b);
}


$time 6 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$Z = serialize ($c);
}


$time 7 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$O = Unserialize ($X);
}


$time 8 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$P = Unserialize ($Y);
}


$time 9 = Microtime (true);


for ($i =1; $i <= $times; $i + +) {
$Q = Unserialize ($Z);
}
$time = Microtime (true);


$var _export_time[' a '] = $time 2-$time 1;
$var _export_time[' b '] = $time 3-$time 2;
$var _export_time[' c '] = $time 4-$time 3;


$serialize _time[' a '] = $time 5-$time 4;
$serialize _time[' b '] = $time 6-$time 5;
$serialize _time[' c '] = $time 7-$time 6;


$unserialize _time[' a '] = $time 8-$time 7;
$unserialize _time[' b '] = $time 9-$time 8;
$unserialize _time[' c '] = $time 10-$time 9;
Print_r ($var _export_time);
Print_r ($serialize _time);
Print_r ($unserialize _time);
?>
Output
Array ([a] => 3.3401498794556 [b] => 5.1394801139832 [c] => 8.8483898639679) Array ([a] => 1.606370 9259033 [b] => 1.7033960819244 [c] => 3.4534389972687) Array ([a] => 1.6037359237671 [b] => 1.81780 3144455 [C] => 3.7992968559265)

The above data indicate:
The performance of the Var_export function is one-fold worse than the performance of the Serialize function, and unserialize time needs to be about the same time as serialize, serialize plus unserialize time, and Var_export time.

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.