简体   繁体   中英

Using an if statement to echo text inside a while loop

I'm working on an assignment right now. The loop is supposed to count to 10 starting from the variable which is 1. When the loop reaches numbers 3 and seven, I need to echo a statement next to the numbers.

Here is my code:

<?php

    $x = 1;
    while($x <= 10) {
        echo "The number is " . $x . "<br />";
        $x = $x + 1;  // increment by 1 … same as $x++;   
    }


    if ($x = $x + 2) {
        echo "<font color='green'>Third time is a charm</font>";
        //  echo "<p>Third time is a charm</p>";
    } else if ($x = $x + 6) {
        echo "<br>";
        echo "<font color='blue'>You got 7! JACKPOT!</font>";
    }

?>

I'm wondering how I would be able to have the echo output next to the statement. I don't know why my if statements aren't currently working right now?

Your if statements are outside your while loop, $x is only ever '10' (err 11) by the time it reaches your conditionals.

If you don't have to use a 'while' loop, you could achieve this cleaner in a for.

for ($i=0; $i<=10; $i++) {
    //conditionals (I would elaborate, but learning by trial and error is great, don't want to rob you of that)
}

If you need to use a while for assignment reasons, just move the brace immediately before the "if" to the end of your script.. ie,

while {
 if() {
 } elseif() {
 }
}

Good luck!

<?php

    $x = 1;
    while($x <= 10) {
        echo "The number is " . $x . "<br />";

        if ($x == 3) {
            echo "<font color='green'>Third time is a charm</font>";
            //  echo "<p>Third time is a charm</p>";
        } else if ($x == 7) {
            echo "<br>";
            echo "<font color='blue'>You got 7! JACKPOT!</font>";
        }

        $x = $x + 1;  // increment by 1 … same as $x++;   
    }

?>

Try this

<?php

//  $i = 1     : start the counter at 1
//  $i <= 10   : execute until 10 is reached
//  $i++       : increment counter by one
for($i = 1; $i <= 10; $i++) {

echo "The number is " . $i;

  if($i == 3) {
    echo "<font color='green'> Third time is a charm</font>";
  } elseif($i == 7) {
    echo "<font color='blue'> You got 7! JACKPOT!</font>";
  }
   echo "<br />";
}

?>

Play around with for loops. They often come in handy.

Your if statements are outside your while loop. The {} represent a block of code that run based on the condition in the () of your while statement, so you need to put them between the { and the ending } . Try something like this:

<?php
$x=0;

// You can add the increment modifier inside the 
// condition too, which will save you a line of code. (Just a shortcut)
while (++$x <= 10) {
    echo "The number is " . $x . "<br />";

    if ($x == 3) {
        echo "<font color='green'>Third time is a charm</font>";
        //  echo "<p>Third time is a charm</p>";
    } else if ($x == 7) {
        echo "<br>";
        echo "<font color='blue'>You got 7! JACKPOT!</font>";
    }
}
?>

or in a switch: (If you want to handle more than just 3 and 7, a switch would probably be the cleanest way.)

<?php
$x=0;

// You can add the increment modifier inside the 
// condition too, which will save you a line of code. (Just a shortcut)
while (++$x <= 10) {
    echo "The number is " . $x . "<br />";

    switch ($x) {
        case 3:
            echo "<font color='green'>Third time is a charm</font>";
            //  echo "<p>Third time is a charm</p>";
            break;
        case 7:
            echo "<br>";
            echo "<font color='blue'>You got 7! JACKPOT!</font>";
            break;
        default:
            // The default logic goes here.

    }
}    
?>

Make sure you use two equal signs to compare values, as a single equal sign will just assign a value to the variable on the left, with the statement on the right.

$x = 1; // Assignment.

Compared to

$x == 1; // Comparison.

PS $x++ is the same as $x = $x + 1 , just a shorthand way of writing it. If the ++ is before the variable (eg ++$x ), the value will be incremented before the statement is evaluated. If it's after (eg $x++ ), the statement will be evaluated first (eg $x <= 10 ), then the value will be incremented after.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM