comparison if statement php code example
Example 1: php conditionals
#Conditionals
<?php
$num = 5;
if($num>4){
if($num < 10){
echo "$num passed<br>";
}
}
if($num >4 && $num <10){
echo "$num passed using Logical operators";
}
$favColor = 'pink';
switch($favColor){
case 'red':
echo '<br>Your favorite color is red';
break;
case 'green':
echo '<br>OH NO!! Your favorite color is green';
break;
case 'blue':
echo '<br>Kool!! Your favorite color is blue';
break;
default:
echo '<br>Yikes! Your favorite color is something else';
}
?>
Example 2: connect an if statement to an input php
re: #80305
Again useful for newbies:
if you need to compare a variable with a value, instead of doing
<?php
if ($foo == 3) bar();
?>
do
<?php
if (3 == $foo) bar();
?>
this way, if you forget a =, it will become
<?php
if (3 = $foo) bar();
?>
and PHP will report an error.