In this article Conditional Statements in PHP we give the information about Conditional statements in PHP are used to decide which code block will be run based on a condition. These make the program more dynamic and interactive.
Conditional Statements in PHP:
- if Statement
The if statement tests a condition. If the condition is true, a block of code is executed.
Syntax
if (condition) {
//This code will run when the condition is true.
}
Example
<?php
$age = 20;
if ($age >= 18) {
echo “You are an adult.”;
}
?>
-
if-else Statement
In an if-else statement, if the condition is not true, an alternative code block is executed.
syntax
if (condition) {
//This code will run when the condition is true.
} else {
//This code will run when the condition is false.
}
Example
<?php
$age = 16;
if ($age >= 18) {
echo “You are an adult.”;
} else {
echo “You are not an adult.”;
}
}
-
if-elseif-else Statement
if-elseif-else is used when more than one condition needs to be checked.
syntax
if(condition1){
//This code will run when condition1 is true.
} elseif (condition2) {
//This code will run when condition1 is false and condition2 is true.
} else {
// This code will run when all the above conditions are false.
}
Example
<?php
$marks = 75;
if ($marks >= 90) {
echo “Your grades: A+”;
} elseif ($marks >= 75) {
echo “Your grade: A”;
} elseif ($marks >= 50) {
echo “Your grade: B”;
} else {
echo “You failed.”;
}
?>
-
switch statement
The switch statement is used when a variable or expression needs to be compared with multiple values. This is a good alternative to if-elseif.
syntax
switch (variable) {
case value1:
//This code will run when the value of the variable is equal to value1.
break;
case value2:
//This code will run when the value of the variable is equal to value2.
break;
default:
// This code will run when none of the above cases match.
}
Example
<?php
$day = “Tuesday”;
switch ($day) {
case “Monday”:
echo “Today is Monday.”;
break;
case “Tuesday”:
echo “Today is Tuesday.”;
break;
case “Wednesday”:
echo “Today is Wednesday.”;
break;
default:
echo “It is an unknown day.”;
}
?>
Difference between Conditional Statements
Statement – When to Use – Attributes
If- when only one condition needs to be checked – Simple and straightforward.
if-else – Alternative code to run if a condition is not true – Provides options.
if-elseif-else – when more than one condition needs to be checked – Useful for checking multiple conditions.
switch – when a variable needs to be compared with multiple values. – Makes code more organized and easier to read.
Tips
- Use brackets: Always write code blocks in {}, even if it is one line of code.
- Write default in switch: If no case matches, then output from the default statement.
- Don’t forget to write break: Use break after each case in switch, otherwise the next cases may also be executed.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining