Home  >  Q&A  >  body text

Problem with numbers in reverse order

Use if, elseif and else statements to modify the output slightly:

When the loop variable $i is 10 and until its value is down to 3, it should simply print the loop variable followed by a newline.

When the loop variable is 2, it should print "Ready!", followed by a newline.

When the loop variable is 1, it should print "Set!", followed by a newline.

When the loop variable is 0, it should print "Go!".


<?php

  for($i = 10; $i >= 0; $i--){

    $j = $i;

    if($j > 2){

      echo $j."\n";

    }elseif($j = 2){

      echo "Ready!\n";

    }elseif($j = 1){

      echo "Set!\n";

    }else{

      echo "Go!";

    }

  }

?>



输出结果为:10 9 8 7 6 5 4 3 Ready! Ready! Ready!


为什么会卡在2那里,求解原理。

P粉079091758P粉079091758538 days ago734

reply all(2)I'll reply

  • P粉079091758

    P粉0790917582023-03-31 18:17:29

    Solved,

    The equal sign requires double

    reply
    1
  • 大瓶可乐@php.cn

    There are also three equal signs, even the types must be the same.

    大瓶可乐@php.cn · 2023-04-01 21:37:14
  • Cancelreply