php中var_dump()函数的详解说明

本文章给大家全面的介绍一下关于php中var_dump()函数用法详解,大家可参考参考.

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

var_dump()方法是判断一个变量的类型与长度,并输出变量的数值,如果变量有值输的是变量的值并回返数据类型,此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值,数组将递归展开值,通过缩进显示其结构.

例1,代码如下:

  1. <?php
  2. $a = "alsdflasdf;a";
  3. $b = var_dump($a);
  4. echo "<br>";
  5. //var_dump($c);
  6. $d=var_dump($c);
  7. echo "<br>";
  8. echo $a;
  9. echo "<br>";
  10. echo $b;
  11. echo "<br>";
  12. //输出:
  13. string(12) "alsdflasdf;a"
  14. NULL
  15. alsdflasdf;a

例2,var_dump() 示例,代码如下:

  1. <?php
  2. $a = array (1, 2, array ("a", "b", "c"));
  3. var_dump ($a);
  4. /* 输出:
  5. array(3) {
  6. [0]=>
  7. int(1)
  8. [1]=>
  9. int(2)
  10. [2]=>
  11. array(3) {
  12. [0]=>
  13. string(1) "a"
  14. [1]=>
  15. string(1) "b"
  16. [2]=>
  17. string(1) "c"
  18. }
  19. }
  20. */
  21. $b = 3.1;
  22. $c = TRUE;
  23. var_dump($b,$c);
  24. /* 输出:
  25. float(3.1)
  26. bool(true)
  27. */
  28. ?>

var_export和serialize做数组缓存

$str = serialize($arr);

这两种机制转换之后的字符串是不一样的,第一种是数组的原型模式,第二种是序列化后的形式,第一 种存入文件中的只要加上<?php ?>标签,就形式了一个可用的数组原型,对调用来说,不用转换,直接返回这个数组就可以,但第二种,则需要再用一次unserialize函数反序列化一下,对于第一种说,就多了一步操作,下来我们用数据说话吧,代码如下:

  1. <?php
  2. set_time_limit(50);
  3. $a = array(1,2,3);
  4. $b = array('a'=>1, 'b'=>2, 'c'=>3);
  5. $c = array('a'=>array(1,2,3), 'b'=>array(4,5,6));
  6. $time1 = microtime(true);
  7. $times = 1000000; #10w
  8. for($i=1; $i<=$times; $i++){
  9. $A = var_export($a, true);
  10. }
  11. $time2 = microtime(true);
  12. for($i=1; $i<=$times; $i++){
  13. $B = var_export($b, true);
  14. }
  15. $time3 = microtime(true);
  16. for($i=1; $i<=$times; $i++){
  17. $C = var_export($c, true);
  18. }
  19. $time4 = microtime(true);
  20. for($i=1; $i<=$times; $i++){
  21. $X = serialize($a);
  22. }
  23. $time5 = microtime(true);
  24. for($i=1; $i<=$times; $i++){
  25. $Y = serialize($b);
  26. }
  27. $time6 = microtime(true);
  28. for($i=1; $i<=$times; $i++){
  29. $Z = serialize($c);
  30. }
  31. $time7 = microtime(true);
  32. for($i=1; $i<=$times; $i++){
  33. $O = unserialize($X);
  34. }
  35. $time8 = microtime(true);
  36. for($i=1; $i<=$times; $i++){
  37. $P = unserialize($Y);
  38. }
  39. $time9 = microtime(true);
  40. for($i=1; $i<=$times; $i++){
  41. $Q = unserialize($Z);
  42. }
  43. $time10 = microtime(true);
  44. $var_export_time['a'] = $time2 - $time1;
  45. $var_export_time['b'] = $time3 - $time2;
  46. $var_export_time['c'] = $time4 - $time3;
  47. $serialize_time['a'] = $time5 - $time4;
  48. $serialize_time['b'] = $time6 - $time5;
  49. $serialize_time['c'] = $time7 - $time6;
  50. $unserialize_time['a'] = $time8 - $time7;
  51. $unserialize_time['b'] = $time9 - $time8;
  52. $unserialize_time['c'] = $time10 - $time9;
  53. print_r($var_export_time);
  54. print_r($serialize_time);
  55. print_r($unserialize_time);
  56. ?>
  57. //output:
  58. //开源代码phpfensi.com
  59. 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时间差不多.