//
Conditional statements are fundamental building blocks in any programming language. They allow your code to make decisions based on certain conditions, enabling it to execute different paths of logic depending on the situation. In PHP, conditional statements are used extensively to control the flow of execution in scripts.
This tutorial will cover three types of conditional statements in PHP: if, else, and elseif (or else if). These statements help you create dynamic and interactive applications by allowing your code to respond differently based on input or other conditions.
The if statement is the most basic form of a conditional statement. It executes a block of code only if a specified condition evaluates to true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
- **Condition:** This is an expression that evaluates to either `true` or `false`.
- **Code Block:** The statements inside the curly braces `{}` are executed only if the condition is true.
### Else Statement
The `else` statement provides an alternative path of execution when the `if` condition is false. It allows you to specify a block of code that should run if the initial condition fails.
**Syntax:**
```php
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
### Elseif Statement
The `elseif` statement allows you to check multiple conditions sequentially. It provides an additional path of execution between the initial `if` and the final `else`.
**Syntax:**
```php
if (condition1) {
// Code to execute if condition1 is true
} elseif (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if all conditions are false
}
## Examples
Let's explore these conditional statements with practical examples.
### Basic If Statement
Here's a simple example that checks if a number is positive:
<CodeBlock language="php">
{`$number = 5;
if ($number > 0) {
echo "The number is positive.";
}`}
</CodeBlock>
**Output:**
<OutputBlock>
{`The number is positive.`}
</OutputBlock>
### If-Else Statement
In this example, we'll check if a number is positive or negative:
<CodeBlock language="php">
{`$number = -3;
if ($number > 0) {
echo "The number is positive.";
} else {
echo "The number is negative.";
}`}
</CodeBlock>
**Output:**
<OutputBlock>
{`The number is negative.`}
</OutputBlock>
### If-Elseif-Else Statement
This example demonstrates checking multiple conditions:
<CodeBlock language="php">
{`$number = 0;
if ($number > 0) {
echo "The number is positive.";
} elseif ($number < 0) {
echo "The number is negative.";
} else {
echo "The number is zero.";
}`}
</CodeBlock>
**Output:**
<OutputBlock>
{`The number is zero.`}
</OutputBlock>
### Nested If Statements
You can also nest `if` statements inside each other to create more complex conditions:
<CodeBlock language="php">
{`$number = 10;
if ($number > 5) {
if ($number < 20) {
echo "The number is between 5 and 20.";
}
}`}
</CodeBlock>
**Output:**
<OutputBlock>
{`The number is between 5 and 20.`}
</OutputBlock>
### Using Logical Operators
Logical operators like `&&` (AND), `||` (OR), and `!` (NOT) can be used to combine multiple conditions:
<CodeBlock language="php">
{`$age = 18;
$isCitizen = true;
if ($age >= 18 && $isCitizen) {
echo "You are eligible to vote.";
} else {
echo "You are not eligible to vote.";
}`}
</CodeBlock>
**Output:**
<OutputBlock>
{`You are eligible to vote.`}
</OutputBlock>
## What's Next?
In the next section, we will explore another type of conditional statement called the `switch` case. The `switch` statement provides a more concise way to handle multiple conditions compared to nested `if-elseif` statements.
Stay tuned for more tutorials on PHP control flow and other essential concepts!