doue1925 2015-06-08 17:37
浏览 16
已采纳

为什么打印和回声在“for”循环中表现不同[重复]

This question already has an answer here:

If I use print in this code:

<?php
for($i = 1; $i <= 3; print $i . "
") {
  $i++; 
}   
?>

I see as output this:

2

3

4

But when I use echo the code doesn't work:

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

I see this error:

PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ')' in /media/datos/xampp/htdocs/temp/1.php on line 3

My question is:

  • Why can I use print as a third expression in a for loop, but cannot when using echo and why do they behave differently from each other?

References:

</div>
  • 写回答

1条回答 默认 最新

  • doulun1939 2015-06-08 18:14
    关注

    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";
    

    Some part of my answer are part of below answer. I think this is the answer of your question. Most important part is print() behaves like a function

    see this answer: https://stackoverflow.com/a/234255/1848929

    What about echo:

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

    see notes part on this page: http://us2.php.net/manual/en/function.echo.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应