Conditional statements are a fundamental concept in programming that allow you to execute different blocks of code based on certain conditions. In PHP, the primary conditional statements are if
, else
, elseif
, and switch
.
- The
if
Statement
if
StatementThe if
statement is used to execute a block of code only if a specified condition is true.
Syntax:
Example:
Explanation:
- The condition
$age >= 18
is evaluated. - If the condition is true, the code inside the
if
block is executed, printing "You are an adult."
- The
else
Statement
else
StatementThe else
statement is used to execute a block of code if the condition in the if
statement is false.
Syntax:
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
Example:
Explanation:
- If
$age >= 18
is false, the code inside theelse
block is executed, printing "You are a minor."
- The
elseif
Statement
elseif
StatementThe elseif
statement is used to specify a new condition to test if the first condition is false.
Syntax:
if (condition1) { // code to be executed if condition1 is true } elseif (condition2) { // code to be executed if condition1 is false and condition2 is true } else { // code to be executed if both condition1 and condition2 are false }
Example:
$score = 75; if ($score >= 90) { echo "Grade: A"; } elseif ($score >= 80) { echo "Grade: B"; } elseif ($score >= 70) { echo "Grade: C"; } else { echo "Grade: F"; }
Explanation:
- The conditions are checked sequentially.
- If
$score >= 90
is false, it checks$score >= 80
, and so on. - The first true condition's block is executed, printing "Grade: C" for a score of 75.
- The
switch
Statement
switch
StatementThe switch
statement is used to perform different actions based on different conditions. It is often used when you have multiple conditions that depend on the same variable.
Syntax:
switch (n) { case value1: // code to be executed if n=value1 break; case value2: // code to be executed if n=value2 break; default: // code to be executed if n is different from all values }
Example:
$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 "Today is not Monday, Tuesday, or Wednesday."; }
Explanation:
- The value of
$day
is compared against each case. - If
$day
matches "Tuesday", the corresponding block is executed, printing "Today is Tuesday." - The
break
statement prevents the code from running into the next case.
Practical Exercises
Exercise 1:
Write a PHP script that checks if a number is positive, negative, or zero.
Solution:
$number = -5; if ($number > 0) { echo "The number is positive."; } elseif ($number < 0) { echo "The number is negative."; } else { echo "The number is zero."; }
Exercise 2:
Create a PHP script that assigns a grade based on a student's score using a switch
statement.
Solution:
$score = 85; switch (true) { case ($score >= 90): echo "Grade: A"; break; case ($score >= 80): echo "Grade: B"; break; case ($score >= 70): echo "Grade: C"; break; case ($score >= 60): echo "Grade: D"; break; default: echo "Grade: F"; }
Common Mistakes and Tips:
- Forgetting the
break
statement inswitch
cases: This can cause the code to fall through to the next case. - Not using curly braces
{}
inif
statements: Always use curly braces to avoid logical errors, even for single-line statements. - Using
=
instead of==
for comparison: Remember that=
is for assignment, while==
is for comparison.
Conclusion
In this section, you learned about the different types of conditional statements in PHP, including if
, else
, elseif
, and switch
. These statements allow you to control the flow of your program based on different conditions. Practice using these statements to become more comfortable with decision-making in your PHP scripts. Next, we will explore loops in PHP, which are essential for executing repetitive tasks.
PHP Programming Course
Module 1: Introduction to PHP
- What is PHP?
- Setting Up the Development Environment
- Your First PHP Script
- PHP Syntax and Variables
- Data Types in PHP
Module 2: Control Structures
Module 3: Functions
- Defining and Calling Functions
- Function Parameters and Return Values
- Variable Scope
- Anonymous Functions and Closures
Module 4: Arrays
Module 5: Working with Forms
Module 6: Working with Files
Module 7: Object-Oriented Programming (OOP)
- Introduction to OOP
- Classes and Objects
- Properties and Methods
- Inheritance
- Interfaces and Abstract Classes
- Traits
Module 8: Working with Databases
- Introduction to Databases
- Connecting to a MySQL Database
- Performing CRUD Operations
- Using PDO for Database Interaction
- Database Security
Module 9: Advanced PHP Techniques
- Error and Exception Handling
- Sessions and Cookies
- Regular Expressions
- Working with JSON and XML
- PHP and Web Services
Module 10: PHP Frameworks and Best Practices
- Introduction to PHP Frameworks
- Getting Started with Laravel
- MVC Architecture
- Best Practices in PHP Development
- Testing and Debugging