加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 18:02:25 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 How are echo and print different in PHP? 5个 如果我在此代码中使用print: ?phpfor($i = 1; $i = 3; print $i . "n") { $i++; } ? 我看到输出这个: 2 3 4 但是当我使用echo时,代码不起作用: ?phpfor($i = 1; $i = 3; echo $i . "n") { $
参见英文答案 > 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读