本文章給大家全面的介紹一下關於php中var_dump()函數用法詳解,大家可參考參考。
var_dump()
void var_dump ( mixed expression [, mixed expression [, ...]] )
var_dump()方法是判斷一個變量的類型與長度,並輸出變量的數值,如果變量有值輸的是變量的值並回返數據類型.
此函數顯示關於一個或多個表達式的結構信息,包括表達式的類型與值。數組將遞歸展開值,通過縮進顯示其結構。
例1
$a = "alsdflasdf;a";
$b = var_dump($a);
echo "
";
//var_dump($c);
$d=var_dump($c);
echo "
";
echo $a;
echo "
";
echo $b;
echo "
";
輸出:
string(12) "alsdflasdf;a"
NULL
alsdflasdf;a
例2
1. var_dump() 示例
代碼如下 復制代碼
$a = array (1, 2, array ("a", "b", "c")); var_dump ($a);
/* 輸出: 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);
/* 輸出: float(3.1) bool(true)
*/ ?>
var_export和serialize做數組緩存
$str = serialize($arr); 這兩種機制轉換之後的字符串是不一樣的,第一種是數組的原型模式,第二種是序列化後的形式。第一 種存入文件中的只要加上標簽,就形式了一個可用的數組原型,對調用來說,不用轉換,直接返回這個數組就可以,但第二種,則需要再用一次unserialize函數反序 列化一下。對於第種一說,就多了一步操作。下來我們用數據說話吧:
代碼如下 復制代碼
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));
$time1 = microtime(true);
$times = 1000000; #10w
for($i=1; $i<=$times; $i++){ $A = var_export($a, true); }
$time2 = microtime(true);
for($i=1; $i<=$times; $i++){ $B = var_export($b, true); }
$time3 = microtime(true);
for($i=1; $i<=$times; $i++){ $C = var_export($c, true); }
$time4 = microtime(true);
for($i=1; $i<=$times; $i++){ $X = serialize($a); }
$time5 = microtime(true);
for($i=1; $i<=$times; $i++){ $Y = serialize($b); }
$time6 = microtime(true);
for($i=1; $i<=$times; $i++){ $Z = serialize($c); }
$time7 = microtime(true);
for($i=1; $i<=$times; $i++){ $O = unserialize($X); }
$time8 = microtime(true);
for($i=1; $i<=$times; $i++){ $P = unserialize($Y); }
$time9 = microtime(true);
for($i=1; $i<=$times; $i++){ $Q = unserialize($Z); } $time10 = microtime(true);
$var_export_time['a'] = $time2 - $time1; $var_export_time['b'] = $time3 - $time2; $var_export_time['c'] = $time4 - $time3;
$serialize_time['a'] = $time5 - $time4; $serialize_time['b'] = $time6 - $time5; $serialize_time['c'] = $time7 - $time6;
$unserialize_time['a'] = $time8 - $time7; $unserialize_time['b'] = $time9 - $time8; $unserialize_time['c'] = $time10 - $time9; 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.6063709259033 [b] => 1.7033960819244 [c] => 3.4534389972687)Array( [a] => 1.6037359237671 [b] => 1.817803144455 [c] => 3.7992968559265)
由上面數據說明: var_export函數性能比serialize函數性能差一倍,而 unserialize時間也需要和serialize差不多的時間,serialize加上unserialize時間,和用var_export時間 差不多。