advertisement
Conditional Statement in PHP with examples
Conditional Statement
A statement is the smallest element of any programming language and normally consists of a command given by a programmer to the computer. A PHP script consists of a series of statements. These statements can be an assignment, a function call, a conditional statement, or even an empty statement that does nothing. A statement usually ends with a semicolon and can be either an individual statement or a group of statements within curly braces. A group of statements is also considered a statement by itself.
The statements in a program are executed in a sequence starting from the first to the last. However, conditional control structures can be used to change the order of the control flow in a program. The conditional control structures control the flow of a program as they execute or skip code based on certain criteria. In PHP programming, there are different types of conditional statements such as if and switch statements. In addition, the ternary operator is also used in PHP.
Conditional Control Structure
Conditional control structures help to direct the flow of execution in a program at runtime, based on a condition. Conditional control structures supported by PHP are as follows:
- if statement
- switch statement
1. The if Statement
- The if statement is the most common conditional control structure in all programming languages.
- The expression in an if statement is called a true expression.
- If the truth expression evaluates to true, a statement or block of code following the if statement is executed and ignored otherwise.
The syntax for the if statement is as follows:
if (truth expression)
{
Statements to be executed;
}
The if keyword is followed by the truth expression in parentheses. The truth expression can be a Boolean variable, a constant, or an expression that evaluates to TRUE, FALSE, or NULL. If the truth expression evaluates to TRUE, the statements following the if-statement are executed. if the truth expression evaluates to FALSE Or NULL the statements are not executed.
Using the if Statement:-
The if statement executes a block of code only when a specified condition is true. If the condition is false, the program ignores the block of code within the if body and executes statements after the if body. The following example creates a form that accepts the name and age of the user, performs validations to the data present in the form, and uses the if.. else statement to check if the fields in the form are blank.
2. The if...else Statement
- A block of code can be executed even when a specified condition is false.
- This can be done using the else statement.
- The else statement is used along with the if statement.
The syntax for the if...else statement is as follows:
if (truth expression){
statements to be executed if the condition evaluates to true;
}
else{
statements to be executed if the condition evaluates to false;
}
3. Nested if Statements
- An if statement can also be included within an if statement or an else statement.
- These are known as nested if statements.
4. switch Statements
- A switch construct can be used as an alternative to a lengthy if...else construct.
- A switch statement consists of an expression that is compared to all possible case expressions listed in its body.
- On finding a match, it executes the block of code ignoring any further case lines.
- Comparison to find the right match is done internally using the equality operator (==) and not the identical operator (===).
- A break statement can also be used to halt the execution of the switch statement and transfer control to code following the switch construct.
The switch statement is followed by a variable in parenthesis. The case keyword is followed by a case constant. The case constant can be an integer or the data type of the case constant must match the data type of the switch variable. Before executing the switch statement, a value should be assigned to the switch variable. Before executing the switch statement, a value should be assigned to the switch variable. The switch statement implements code line by line. The switch statement executes the code only when the value of the case constant matches the value of the switch variable. The program continues to execute statements until the end of the switch statement or until a break statement is encountered.
The break statement is used to move the control to statements following the switch statement. It instructs a program to halt the execution, come out of the switch statement, and execute statements following the switch construct. In the absence of the break statement, the program executes all the statements including the statements of the following cases in the switch construct. The default is a special case. It is used when none of the case constants match the value of the switch variable. The default case is placed at the end of all the cases in the switch statement.
A switch statement evaluates a condition only once and the result is compared with each case statement to obtain a match. However, an if...else statement evaluates a condition more than once. For complex conditions, a switch statement is preferred to an if...else statement.
Using the switch Statement:-
A switch statement checks the value present in a variable against multiple values and executes a block of code based on the value it matches. The value can be an integer or string constant. The switch statement is easier to read than multiple if statements.
To demonstrate the usage of the switch statement, consider an example where an organization provides various facilities and perks to their employees depending on the grade assigned to them. The table lists all facilities available for respective grades.
5. Ternary (?) Operator
- The ternary operator is also known as a conditional operator.
- It simplifies complex conditions into one-line statements.
The syntax for the ternary Operator is as follows:
truth_exp ? expr1 : expr2;
A ternary operator requires three operands. The operator evaluates the truth_expr and if it is true, exprl is evaluated and if it is false, expr2 is evaluated.
The ternary operator is considered as an alternative for the if.. else statement. This is demonstrated in the following Code Snippets to find the greatest of two numbers using the if...else statement as well as a ternary operator.
Using the Ternary Operator:
The ternary operator is also known as the conditional operator. It simplifies complex conditions into single-line statements. The ternary operator evaluates the expression for a True or False value and executes one of the two given statements. It is considered a shortcut for an if...else statement. To demonstrate the usage of the ternary operator, modify dispDet. PHP file to display the same message using the ternary operator.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0