0% found this document useful (0 votes)
13 views8 pages

Conditional Statements in PHP: by Monika Singla

Uploaded by

jaimahad4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Conditional Statements in PHP: by Monika Singla

Uploaded by

jaimahad4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Conditional

Statements in PHP
Conditional statements control the flow of execution in your PHP
code. They allow you to execute different code blocks based on
the evaluation of certain conditions. Let's dive in and explore
the various types of conditional statements available in PHP.

By monika singla
The if() and elseif() Condition Statements

1 if() Statement

1 f code only if the


The if() statement executes a block
given condition evaluates to true.

2 elseif() Statement

The elseif() statement provides an additional condition to be checked if the


previous if() or elseif() conditions are false.

3 else Statement

The else statement provides a fallback block of code to


be executed if all previous if() and elseif() conditions
evaluate to false.
Examples of if() and elseif() Statements
Example 1 Example 2

Check if a number is positive, negative, or zero. Check if a user is logged in.

''' '''
$number = -5; session_start();

if ($number > 0) { if (isset($_SESSION['user_id'])) {


echo 'The number is positive.'; echo 'Welcome, ' . $_SESSION['username'];
} elseif ($number < 0) { } else {
echo 'The number is negative.'; echo 'Please log in.';
} else { }
echo 'The number is zero.'; '''
}
'''
The switch Statement in PHP

Initialization Code Execution

The switch statement begins with a variable If a match is found, the corresponding code
or expression to evaluate. block for that case is executed.

1 3

2 4

Case Matching Default Block


The switch statement compares the The default block is optional and executed if
variable's value to the values specified in no other case matches the variable's value.
each case statement.
Using the Ternary (?) Operator in PHP
Syntax Structure Example
The ternary operator is a shorthand condition ? expression_if_true : This code checks if a number is
way of writing if-else statements in expression_if_false even or odd and assigns the result
a single line of code. to a variable.
'''
$number = 7;
$result = ($number % 2 ==
0) ? 'Even' : 'Odd';
echo $result; // Output:
Odd
'''
The while() Loop in PHP
Initialization
A variable is initialized before the loop begins. This
variable is usually used as the loop counter.

Condition
The loop continues to execute as long as the
specified condition remains true.

Iteration
The loop body is executed repeatedly, and the loop
counter is updated within the loop.
The do-while Statement in PHP
while() do-while()

The condition is checked before the loop body is The loop body is executed at least once, regardless
executed. If the condition is false, the loop body is of the condition, and the condition is checked after
never executed. the loop body is executed.
The for() Loop in PHP

Initialization
A variable is initialized before the loop begins. This variable is usually used
as the loop counter.

Condition
The condition is checked at the beginning of each iteration. The loop
continues to execute as long as the condition remains true.

Increment
The loop counter is incremented or decremented after each iteration.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy