Introduction
If-else helps your program choose what to do based on conditions. It makes decisions like you do in real life.
If-else helps your program choose what to do based on conditions. It makes decisions like you do in real life.
<?php if (condition) { // code to run if condition is true } else { // code to run if condition is false } ?>
The condition is a statement that is either true or false.
Only one block runs: either the if part or the else part.
<?php $age = 18; if ($age >= 18) { echo "You can vote."; } else { echo "You are too young to vote."; } ?>
<?php $number = -5; if ($number > 0) { echo "Positive number."; } else { echo "Not a positive number."; } ?>
This program checks the temperature. If it is above 25, it says it's hot; otherwise, it says it is not hot.
<?php $temperature = 30; if ($temperature > 25) { echo "It's hot outside."; } else { echo "It's not hot outside."; } ?>
You can add more conditions using elseif for multiple choices.
Make sure your conditions are clear and simple to avoid confusion.
If-else lets your program pick between two paths based on a condition.
Only one block runs: either the if or the else.
Use it to make your program smart and responsive to different situations.
if-else statement do in PHP?$score = 75;
if ($score >= 60) {
echo "Pass";
} else {
echo "Fail";
}if ($age > 18)
echo "Adult";
else
echo "Minor"Pass the test and this pattern moves unknown