[英]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.
循环应该从变量 1 开始计数到 10。当循环到达数字 3 和 7 时,我需要在数字旁边回显一条语句。
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.我想知道如何在语句旁边显示 echo 输出。 I don't know why my if statements aren't currently working right now?
我不知道为什么我的 if 语句现在不起作用?
Your if statements are outside your while loop, $x is only ever '10' (err 11) by the time it reaches your conditionals.你的 if 语句在你的 while 循环之外,当它达到你的条件时, $x 只是'10'(err 11)。
If you don't have to use a 'while' loop, you could achieve this cleaner in a for.如果您不必使用“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 you need to use a while for assignment reasons, just move the brace immediately before the "if" to the end of your script.. ie,如果由于分配原因需要使用一段时间,只需将“if”之前的大括号移动到脚本末尾..即,
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.玩转 for 循环。 They often come in handy.
它们经常派上用场。
Your if
statements are outside your while
loop.你的
if
语句在你的while
循环之外。 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 }
. {}
表示基于while
语句()
中的条件运行的代码块,因此您需要将它们放在{
和结尾}
。 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.)或在 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.
}
}
?>
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. PS
$x++
与$x = $x + 1
,只是一种简写方式。 If the ++
is before the variable (eg ++$x
), the value will be incremented before the statement is evaluated.如果
++
在变量之前(例如++$x
),则该值将在评估语句之前递增。 If it's after (eg $x++
), the statement will be evaluated first (eg $x <= 10
), then the value will be incremented after.如果它在之后(例如
$x++
),该语句将首先被评估(例如$x <= 10
),然后该值将在之后递增。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.