标签:
1、echo()、print()、print_r()和var_dump()的区别:
相同点:
?、笼统的概括为他们都可以输出变量内容
不同点:
?、echo():输出一个或者多个字符串或变量值,它是一个语言结构而非函数。因为他不是函数,所以它没有返回值
1 <?php 2 for ($i = 97; $i <= 103; $i++) { 3 $arr[chr($i)] = $i . PHP_EOL; 4 } 5 extract($arr); 6 echo ‘a = ‘ . $a, ‘b = ‘ . $b, ‘c = ‘ . $c; 7 echo(‘d = ‘ . $d); 8 echo "e = $e"; 9 echo ‘f = $f‘ . PHP_EOL; 10 echo (‘g = ‘) . $g; 11 12 运行结果: 14 a = 97 15 b = 98 16 c = 99 17 d = 100 18 e = 101 19 f = $f 20 g = 103
?、print():输出一个或多个字符串或变量值。它只能打印出简单类型变量的值(如:int、string等),不能打印数组和对象。它本质上也是一个语言结构而非函数,因此它无法被“变量函数”调用,速度稍慢于echo
1 <?php 2 $arr = [ 3 ‘a‘ => ‘string‘ . PHP_EOL, 4 ‘b‘ => 2015 . PHP_EOL, 5 ‘c‘ => null . PHP_EOL, 6 ‘d‘ => 65 . PHP_EOL, 7 ]; 8 extract($arr); 9 print $a; 10 print($b); 11 $d = print($d); 12 print $d . PHP_EOL; 13 14 运行结果: 15 string 16 2015 17 65 18 1
?、print_r():输出复杂类型变量的值(如:数组、对象、资源等),是函数,有返回值
1 <?php 2 $arr = [ 3 ‘string‘, 4 2015, 5 null, 6 3.1415926, 7 true, 8 (new SplQueue()), 9 ]; 10 print_r($arr); 11 12 $var1 = print_r($arr[0]); 13 print_r(PHP_EOL . $var1); 14 $var2 = print_r(PHP_EOL . $arr[1], true); 15 print_r($var2); 16 17 运行结果: 18 Array 19 ( 20 [0] => string 21 [1] => 2015 22 [2] => 23 [3] => 3.1415926 24 [4] => 1 25 [5] => SplQueue Object 26 ( 27 [flags:SplDoublyLinkedList:private] => 4 28 [dllist:SplDoublyLinkedList:private] => Array 29 ( 30 ) 31 32 ) 33 34 ) 35 string 36 1 37 2015
?、var_dump():输出一个或多个表达式的结构信息,包括表达式的类型、值、长度等信息,常用来调试
1 <?php 2 $arr = [ 3 ‘string‘, 4 2015, 5 null, 6 3.1415926, 7 false, 8 (new SplStack()), 9 ]; 10 var_dump($arr); 11 var_dump($arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5]); 12 13 运行结果: 14 array(6) { 15 [0] => 16 string(6) "string" 17 [1] => 18 int(2015) 19 [2] => 20 NULL 21 [3] => 22 double(3.1415926) 23 [4] => 24 bool(false) 25 [5] => 26 class SplStack#1 (2) { 27 private $flags => 28 int(6) 29 private $dllist => 30 array(0) { 31 } 32 } 33 } 34 string(6) "string" 35 int(2015) 36 NULL 37 double(3.1415926) 38 bool(false) 39 class SplStack#1 (2) { 40 private $flags => 41 int(6) 42 private $dllist => 43 array(0) { 44 } 45 } 46 string(6) "string" 47 bool(true)
2、
标签:
原文地址:http://www.cnblogs.com/atblog/p/4767895.html