dongwen7187 2014-01-17 03:53
浏览 34
已采纳

php - 输出中的一些奇怪的行为?

I was just playing around with PHP when this happened. Look at the commented code.

    <?php
     error_reporting(0); //turns of errors and notices (which will be shown otherwise)

     //here we would've gotten a notice saying $_ has no value, which is true. But PHP        automatically gives it the value 0, then we add one to it. ($_++), add $_ and add $_. So it's    1 + 1 + 1 which is two somehow
     echo ($_++ + $_ + $_);

So my question is... why does it output 2?

  • 写回答

1条回答 默认 最新

  • dongzhenyin2001 2014-01-17 03:58
    关注

    Most of the answer is contained in your code.

    $_ is 0 initially. By $_++, you increment $_, setting it to one. Therefore, $_ is 1, but the value of the postincrement (!) $_++ is still 0. The value of ++$_ would be 1.

    Then, you add two times $_ (which is 1), yielding 2 overall.

    See this SO post for a detailed preincrement/postincrement comparison.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化