0

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.

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

Do I need to move my definition of $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!

lmb06e
  • 1
  • 1

5 Answers5

3

There is some syntax error in your code use . for concatenation in 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; .
here is the Difference between if () { } and 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;
?> 
Community
  • 1
  • 1
Haseeb
  • 2,214
  • 1
  • 22
  • 43
1

There are some Syntax errors in your code. You have missed ) as well as concatenation operator i.e. .

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>";}
}
AkshayP
  • 2,141
  • 2
  • 18
  • 27
1
<?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.

Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56
1
<?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>";
    }
}
?>  
Ali Zia
  • 3,825
  • 5
  • 29
  • 77
0

You had missed the closing paranthesis ) for the if and elseif block and the concatenation operator . in the first echo statement. echo accepts and outputs one string and so you need to combine several strings to make it one and ask echo to output it. 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;

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;

This is how it works.

Mathews Mathai
  • 1,707
  • 13
  • 31