我经常使用echo和print_r,几乎从不使用print。
我觉得echo是一个宏,而print_r是var_dump的别名。
但这不是解释差异的标准方式。
我经常使用echo和print_r,几乎从不使用print。
我觉得echo是一个宏,而print_r是var_dump的别名。
但这不是解释差异的标准方式。
打印和回声或多或少是相同的;它们都是显示字符串的语言结构。两者的区别很细微:print的返回值为1,因此可以在表达式中使用,而echo的返回类型为void;Echo可以接受多个参数,尽管这种用法很少见;Echo比print稍快。(就我个人而言,我总是使用echo,从不打印。)
Var_dump打印变量的详细转储,包括变量的类型和任何子项的类型(如果它是数组或对象)。Print_r以更易于阅读的形式打印变量:字符串不加引号,类型信息被省略,数组大小没有给出,等等。
根据我的经验,在调试时,Var_dump通常比print_r更有用。当你不知道变量中有什么值/类型时,它特别有用。考虑这个测试程序:
$values = array(0, 0.0, false, '');
var_dump($values);
print_r ($values);
使用print_r,你不能区分0和0.0,或者false和":
array(4) {
[0]=>
int(0)
[1]=>
float(0)
[2]=>
bool(false)
[3]=>
string(0) ""
}
Array
(
[0] => 0
[1] => 0
[2] =>
[3] =>
)
只是为了补充John的回答,echo应该是您用于将内容打印到页面的唯一一个。
打印速度稍慢。Var_dump()和print_r()应该只用于调试。
另外值得一提的是,print_r()和var_dump()将默认回显,为print_r()添加第二个参数,至少该参数的值为true,以使其返回,例如print_r($array, true)。
echo和return的区别是:
echo:将立即将值打印到输出。 返回:将函数的输出作为字符串返回。对日志记录等有用。
echo
输出一个或多个以逗号分隔的字符串 没有返回值 例如,返回“字符串1”,“字符串2”
打印
只输出一个字符串 返回1,因此可以在表达式中使用 例如,打印“Hello” 或者,if ($expr && print "foo")
print_r ()
输出任何一个值的人类可读表示 不仅接受字符串,还接受包括数组和对象在内的其他类型,并将它们格式化为可读 调试时有用 如果给出第二个可选参数,可以将其输出作为返回值返回(而不是回显)
var_dump()
输出一个或多个用逗号分隔的值的人类可读表示 不仅接受字符串,还接受包括数组和对象在内的其他类型,并将它们格式化为可读 使用不同的输出格式print_r(),例如它也打印值的类型 调试时有用 没有返回值
var_export()
输出任意一个值的人类可读和php可执行表示 不仅接受字符串,还接受包括数组和对象在内的其他类型,并将它们格式化为可读 对print_r()和var_dump()使用不同的输出格式-产生的输出是有效的PHP代码! 调试时有用 如果给出第二个可选参数,可以将其输出作为返回值返回(而不是回显)
注:
Even though print can be used in an expression, I recommend people avoid doing so, because it is bad for code readability (and because it's unlikely to ever be useful). The precedence rules when it interacts with other operators can also be confusing. Because of this, I personally don't ever have a reason to use it over echo. Whereas echo and print are language constructs, print_r() and var_dump()/var_export() are regular functions. You don't need parentheses to enclose the arguments to echo or print (and if you do use them, they'll be treated as they would in an expression). While var_export() returns valid PHP code allowing values to be read back later, relying on this for production code may make it easier to introduce security vulnerabilities due to the need to use eval(). It would be better to use a format like JSON instead to store and read back values. The speed will be comparable.
回音:
它是语句而不是函数 没有返回值
不需要括号
非打印数组
打印
它是实函数
返回类型1
需要括号
非打印数组
Print_r
以人类可读的格式打印
字符串不加引号
不是变量的详细信息,如类型和所有
var_dump
所有变量的转储信息 比如元素和子元素的类型
**回声可以接受多个表达式,而打印不能。 Print_r () PHP函数用于以人类可读的形式返回数组。它被简单地写成
![Print_r ($your_array)][1]
Echo: Echo是一种不需要使用括号的语言构造,它可以接受任意数量的参数并返回void。
void echo (param1,param2,param3.....);
Example: echo "test1","test2,test3";
Print:这是一种不需要使用括号的语言结构,它只接受一个参数并返回
1 always.
int print(param1);
print "test1";
print "test1","test2"; // It will give syntax error
prinf:这是一个函数,它至少接受一个字符串和格式化样式,并返回输出字符串的长度。
int printf($string,$s);
$s= "Shailesh";
$i= printf("Hello %s how are you?",$s);
echo $i;
Output : Hello Shailesh how are you?
27
echo returns void so its execution is faster than print and printf
print_r()可以打印出值,但如果第二个标志参数被传递并且为TRUE -它将返回打印的结果作为字符串,而不发送到标准输出。 var_dump。如果安装了XDebug调试器,那么输出结果将以更易于阅读和理解的方式格式化。
echo、print、print_r和var_dump之间的区别非常简单。
回声
Echo实际上不是一个函数,而是一个用于打印输出的语言结构。它比打印稍微快一点。
echo "Hello World"; // this will print Hello World
echo "Hello ","World"; // Multiple arguments - this will print Hello World
$var_1=55;
echo "$var_1"; // this will print 55
echo "var_1=".$var_1; // this will print var_1=55
echo 45+$var_1; // this will print 100
$var_2="PHP";
echo "$var_2"; // this will print PHP
$var_3=array(99,98,97) // Arrays are not possible with echo (loop or index value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with echo (loop or index value required)
你也可以使用带或不带括号的echo语句
echo ("Hello World"); // this will print Hello World
打印
就像echo构造一样,print也是一种语言构造,而不是真正的函数。echo和print的区别在于print只接受一个参数,而print总是返回1。而echo没有返回值。所以print语句可以用在表达式中。
print "Hello World"; // this will print Hello World
print "Hello ","World"; // Multiple arguments - NOT POSSIBLE with print
$var_1=55;
print "$var_1"; // this will print 55
print "var_1=".$var_1; // this will print var_1=55
print 45+$var_1; // this will print 100
$var_2="PHP";
print "$var_2"; // this will print PHP
$var_3=array(99,98,97) // Arrays are not possible with print (loop or index value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with print (loop or index value required)
就像echo一样,print可以带括号或不带括号使用。
print ("Hello World"); // this will print Hello World
print_r
print_r()函数用于打印关于变量的人类可读信息。 如果参数是一个数组,print_r()函数打印它的键和元素(对象也是如此)。
print_r ("Hello World"); // this will print Hello World
$var_1=55;
print_r ("$var_1"); // this will print 55
print_r ("var_1=".$var_1); // this will print var_1=55
print_r (45+$var_1); // this will print 100
$var_2="PHP";
print_r ("$var_2"); // this will print PHP
$var_3=array(99,98,97) // this will print Array ( [0] => 1 [1] => 2 [2] => 3 )
$var_4=array("P"=>"3","J"=>"4"); // this will print Array ( [P] => 3 [J] => 4 )
var_dump
Var_dump函数通常用于调试和打印关于变量/数组/对象的信息(类型和值)。
var_dump($var_1); // this will print int(5444)
var_dump($var_2); // this will print string(5) "Hello"
var_dump($var_3); // this will print array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
var_dump($var_4); // this will print array(2) { ["P"]=> string(1) "3" ["J"]=> string(1) "4" }