繁体   English   中英

使用 if 语句在 while 循环中回显文本

[英]Using an if statement to echo text inside a while loop

我现在正在处理一项任务。 循环应该从变量 1 开始计数到 ​​10。当循环到达数字 3 和 7 时,我需要在数字旁边回显一条语句。

这是我的代码:

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

?>

我想知道如何在语句旁边显示 echo 输出。 我不知道为什么我的 if 语句现在不起作用?

你的 if 语句在你的 while 循环之外,当它达到你的条件时, $x 只是'10'(err 11)。

如果您不必使用“while”循环,则可以在 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”之前的大括号移动到脚本末尾..即,

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

祝你好运!

<?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++;   
    }

?>

尝试这个

<?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 />";
}

?>

玩转 for 循环。 它们经常派上用场。

你的if语句在你的while循环之外。 {}表示基于while语句()中的条件运行的代码块,因此您需要将它们放在{和结尾} 尝试这样的事情:

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

或在 switch 中:(如果你想处理的不仅仅是 3 和 7,switch 可能是最干净的方式。)

<?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.

    }
}    
?>

确保使用两个等号来比较值,因为一个等号只会为左侧的变量赋值,右侧的语句。

$x = 1; // Assignment.

相比

$x == 1; // Comparison.

PS $x++$x = $x + 1 ,只是一种简写方式。 如果++在变量之前(例如++$x ),则该值将在评估语句之前递增。 如果它在之后(例如$x++ ),该语句将首先被评估(例如$x <= 10 ),然后该值将在之后递增。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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