微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 为什么print和echo在“for”循环中表现不同

参见英文答案 > How are echo and print different in PHP? 5个
如果我在此代码中使用print:
<?PHP
for($i = 1; $i <= 3; print $i . "\n") {
  $i++; 
}   
?>

我看到输出这个:

2

3

4

但是当我使用echo时,代码不起作用:

<?PHP
for($i = 1; $i <= 3; echo $i . "\n") {
  $i++; 
}   
?>

我看到这个错误

PHP Parse error: Syntax error,unexpected ‘echo’ (T_ECHO),expecting ‘)’ in /media/datos/xampp/htdocs/temp/1.PHP on line 3

我的问题是:

>为什么我可以在for循环中使用print作为第三个表达式,但是在使用echo时不能这样做,为什么它们的行为彼此不同?

参考文献:

> http://php.net/echo
> http://php.net/print

Expression. print() behaves like a function in that you can do: $ret
= print “Hello World”; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An
example from the PHP Manual:

$b ? print "true" : print "false";

我的答案的某些部分是以下答案的一部分.我认为这是你问题的答案.最重要的部分是print()就像一个函数

看到这个答案:https://stackoverflow.com/a/234255/1848929

回声怎么样:

Note: Because this is a language construct and not a function,it
cannot be called using variable functions.

请参阅本页的注释部分:http://us2.php.net/manual/en/function.echo.php

原文地址:https://www.jb51.cc/php/134332.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐