start learning
Image 1
53634474460112

If statement

In PHP, an "if" statement is a conditional statement used to execute a block of code if a specified condition evaluates to true.


Basic Syntax of an "if" Statement

The basic syntax of an "if" statement in PHP is as follows:


if (condition) {
    // Code to execute if the condition is true
}
 

Simple "if" Statement


$age = 25;
if ($age >= 18) {
    echo "You are an adult.";
}
 


"if" and "else" Statement


$score = 65;
if ($score >= 70) {
    echo "You passed!";
} else {
    echo "You failed.";
}
 


"if" Statement with Multiple Conditions


$temperature = 28;
if ($temperature < 32) {
    echo "It's freezing!";
}
 


In each of these examples, the "if" statement evaluates the condition, and if the condition is met (true), the code inside the "if" block is executed. If the condition is not met (false), the code inside the "if" block is skipped (in the absence of an "else" block).


×