An if statement runs code only when its condition evaluates to true. Use strict comparisons such as === and !== when both value and type matter.
<?php
$score = 82;
if ($score >= 90) {
echo 'A';
} elseif ($score >= 75) {
echo 'B';
} else {
echo 'C';
}
?>

$j = 5;<br>$k = 10;
if ($j < $k){<br>print "Variable $j value is less than $k";
}
We can add ELSE condition to the above statement and that part will be executed
once the if condition does not satisfy. $j = 10;<br>$k = 5;
if ($j < $k){<br>print "Variable $j value is less than $k";
<b>// This line will not be executed
}
else{
print "Variable $j value is greater than $k";
<b>// This line will be executed
}
$j=8;
$k=5;
if($j<$k){
echo " Variable \$j $j value is less than \$k $k";
}elseif($j>10){
echo " Variable \$j $j value is greater than 10";
}else{
echo " variable \$j $j is less than 10 but greater than 5";
}
Output is
variable $j 8 is less than 10 but greater than 5
Once all the conditions are False the last line is executed. Variable $j 12 value is greater than 10
$mark=65;
$status=$mark > 50 ? 'Pass':'Fail';
echo $status; # output is Pass
We can receive name from Query string and use the same inside our script.
$name=strlen($_GET['name'])>2 ? $_GET['name'] : 'anonymous';
print($name);
$mark=80;
if($mark >= 50){
echo " You Passed, ";
if ($mark >=80){
echo " You got Distinction also";
} // end of inner if condition
} // end of outer if condition
if (isset($_SESSION['userid']&& !empty($_SESSION['userid']))) {
// Welcome message
}else {
// Ask the user to login
}
What is session variable ?
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.