Home  >  Article  >  php教程  >  浅析echo(),print(),print_r(),return之间的区别

浅析echo(),print(),print_r(),return之间的区别

WBOY
WBOYOriginal
2016-06-13 10:18:17855browse

PHP中echo(),print(),print_r(),return的区别?

往往有很多新手和不太使用print()的朋友就答不好这个问题 从而面试失败。

那么究竟她们有什么区别呢?下面我们来看看

答案:
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)
print() 只能打印出简单类型变量的值(如int,string)
print_r() 可以打印出复杂类型变量的值(如数组,对象)
return 语句 从当前函数退出,并从那个函数返回一个值。

echo 输出一个或者多个字符串
echo — 输出一个或者多个字符串
Descrīption
void echo ( string arg1 [, string ...] ) //返回值为空
echo “你好”,” 朋友”;

print –输出一个字符串
Descrīption
int print ( string arg )//返回值为整形
print “你好朋友”;

可以进行下面操作
$name=print “nihao /n”;
$str = ‘test print value is $name .';
eval(“/$print=/”$str/”;”);
echo $print;

print_r — 打印关于变量的易于理解的信息。
bool print_r ( mixed expression [, bool return] ) //返回值是布尔型的,参数是mix类型的,可以是字符串,整形,数组,对象类print_r() 显示关于一个变量的易于理解的信息。如果给出的是 string、integer 或 float,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素。object 与数组类似。

注: print_r() 将把数组的指针移到最后边

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn