我经常使用echo和print_r,几乎从不使用print。

我觉得echo是一个宏,而print_r是var_dump的别名。

但这不是解释差异的标准方式。


当前回答

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()用于以人类可读的格式打印数组。

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.

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

只是为了补充John的回答,echo应该是您用于将内容打印到页面的唯一一个。

打印速度稍慢。Var_dump()和print_r()应该只用于调试。

另外值得一提的是,print_r()和var_dump()将默认回显,为print_r()添加第二个参数,至少该参数的值为true,以使其返回,例如print_r($array, true)。

echo和return的区别是:

echo:将立即将值打印到输出。 返回:将函数的输出作为字符串返回。对日志记录等有用。

它们都是语言结构。Echo返回void, print返回1。Echo被认为比print稍快。