doupeng5320 2019-03-11 04:10
浏览 36
已采纳

为什么1没有被输出? (简单的php问题)

I have a question which I don't know the answer of. I've been thinking about it for a while.

The following code:

$i = 1;
while($i < 10) 
    if(($i++) % 2 == 0)
    echo $i;

It correctly outputs 3579, but why isnt 1 also included in the output?

I'm a beginner with PHP and am looking forward for someone to help me.

Thank you very much! :D

  • 写回答

1条回答 默认 最新

  • douyou7102 2019-03-11 04:13
    关注

    Two modifications:

    $i = 0; // Make it 0 from 1
    while($i < 10)
    if(($i++) % 2 == 0)
    echo "<br/>".$i; // Make $i instead of $1
    
    Output:
    
    
    1
    3
    5
    7
    9
    

    Program hand run:

    1) Set $i to 0.

    2) If it is greater than 10, go ahead.

    3) Increment it by 1

    4) So, for $i => 0->1, 1->2

    4) if new $i is even, print it. (So for first loop iteration, you are have $i -> 1 instead of 0 because of ++$i

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

报告相同问题?

悬赏问题

  • ¥15 损失匹配问题,求解答
  • ¥15 3500常用汉字书法体检测数据集下载
  • ¥15 odoo17在制造模块或采购模块良品与次品如何分流和在质检模块下如何开发
  • ¥15 Qt音乐播放器的音乐文件相对路径怎么写
  • ¥15 VB.NET利用摄像头拍照的程序
  • ¥15 linux下vscode设置不了字连体
  • ¥20 游戏mod是如何制作的
  • ¥15 关于#hadoop#的问题:按照老师上课讲的步骤写的
  • ¥20 有人会用这个工具箱吗 付fei咨询
  • ¥30 成都市武侯区住宅小区兴趣点