简体   繁体   English

如何在PHP中修复以下For循环和If语句组合?

[英]How Can I Fix the Following For-loop and If statement combination in PHP?

Edit: Solved! 编辑:解决了! Thank you all for pointing out the missing concatenation operator and missing punctuation. 谢谢大家指出缺少的串联运算符和缺少的标点符号。

I've just been introduced to PHP and am trying to work on a basic exercise where I use a for-loop in combination with several if/elseif statements. 我刚刚被介绍给PHP,并且正在尝试进行一个基础练习,在该练习中,我将for循环与几个if / elseif语句结合使用。

My variables A and B are related mathematically, and I need the output to state the value of B along with a simple word ('Good', 'bad', 'okay'- depending on the resultant value of B). 我的变量A和B在数学上相关,并且我需要输出来声明B的值以及一个简单的单词(“好”,“坏”,“好”-取决于B的结果值)。 I'd also like each result of the loop to be printed on a new line. 我还希望将循环的每个结果打印在新行上。

My attempt can be seen below: 我的尝试可以在下面看到:

<?php

for ($A = 0; $A <= 100; $A++){
$B = (2/3) * ($A - 25); 

if ($B < 0 {
echo $B ", Bad <br>";}

elseif ($B > 45 {
echo $B ", Good <br>";}

else {
echo $B ", Okay <br>";}
}

endfor;
?>  

This is only my second day of working with PHP (and the first time I've written a loop statement of any kind in about seven years) so I apologize if my errors are glaring. 这只是我使用PHP的第二天(这是大约七年来我第一次编写任何形式的循环语句),因此,如果我的错误很明显,我深表歉意。

Do I need to move my definition of $B? 我是否需要移动$ B的定义? Do I need to split this statement up somehow? 我是否需要以某种方式拆分此语句? Is it my echo statements that are the issue? 这是我的回声语句吗? Any hints or explanations are greatly appreciated. 任何提示或解释将不胜感激。 Thanks! 谢谢!

There is some syntax error in your code use . 您的代码使用中存在一些语法错误. for concatenation in PHP PHP中串联

and there is two types syntax for control structures detail link , either use if( some condition ){ your code } or if(some condition): "your code" endif; 控制结构详细链接有两种类型的语法,可以使用if( some condition ){ your code }if(some condition): "your code" endif; .
here is the Difference between if () { } and if () : endif; 这是if(){}和if()之间区别:endif;

<?php

for ($A = 0; $A <= 100; $A++):
$B = (2/3) * ($A - 25); 

    if($B < 0 ){
        echo $B .", Bad <br>";
    }else if ($B > 45) {
        echo $B .", Good <br>";
    }else{
        echo $B .", Okay <br>";
    }

endfor;
?> 

There are some Syntax errors in your code. 您的代码中有一些语法错误。 You have missed ) as well as concatenation operator ie . 您已经错过了)以及串联运算符,例如.

if ($B < 0 {    //  if ($B < 0)  .... ")" is missing
    echo $B ", Bad <br>";}    //  echo $B .", Bad <br>";}  .... "." is missing

here is corrected code : 这是更正的代码:

for ($A = 0; $A <= 100; $A++){
    $B = (2/3) * ($A - 25); 

    if ($B < 0) {
        echo $B . " Bad <br>";
    }

elseif ($B > 45) {
echo "Good <br>";}

else {
echo "Okay <br>";}
}
<?php

for ($A = 0; $A <= 100; $A++){
$B = (2/3) * ($A - 25); 

if ($B < 0) {
echo $B .", Bad <br>";
}

elseif ($B > 45) {
echo $B ."Good <br>";
}

   else {
   echo $B ."Okay <br>";
  }
}
?>  

Try this code. 试试这个代码。

<?php

for ($A = 0; $A <= 100; $A++) {
    $B = (2 / 3) * ($A - 25);
    if ($B < 0) {
        echo $B . " Bad <br>";
    } else if ($B > 45) {
        echo "Good <br>";
    } else {
        echo "Okay <br>";
    }
}
?>  

You had missed the closing paranthesis ) for the if and elseif block and the concatenation operator . 您已经错过了ifelseif块以及串联运算符的结束括号( ) . in the first echo statement. 在第一个echo语句中。 echo accepts and outputs one string and so you need to combine several strings to make it one and ask echo to output it. echo接受并输出一个字符串,因此您需要组合多个字符串以使其成为一个字符串,并要求echo将其输出。 endfor; is also not required. 也不需要。

<?php

for ($A = 0; $A <= 100; $A++)
{ 
$B = (2/3) * ($A - 25);
if ($B < 0) 
{ 
echo $B.", Bad <br>";
}
elseif ($B > 45 )
{
 echo "Good <br>";
}
else 
{ 
echo "Okay <br>";
} 
}
?>

Using endfor; 使用endfor;

for ($A = 0; $A <= 100; $A++):
$B = (2/3) * ($A - 25);
if ($B < 0) 
{ 
echo $B.", Bad <br>";
}
elseif ($B > 45 )
{
 echo "Good <br>";
}
else 
{ 
echo "Okay <br>";
} 
endfor;

I have replaced the opening { of the for loop with : and the closing brace with endfor; 我已经取代了开口{的用于与环:以及与右大括号endfor;

This is how it works. 这就是它的工作方式。

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

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