PHP If Statement Multiple Conditions
Last Updated : Mar 11, 2024
In this tutorial we will show you the solution of PHP if statement multiple conditions, making choices is an inevitable part of life. From trivial considerations like what to dress to life-altering decisions like work and family, there's a lot to think about.
In development, it's the same a program must be able to respond to some form of input in order to be helpful.
When a visitor clicks on a website's contact button, they expect to be taken to a contact page.
If nothing happens or they are directed to the incorrect page, the user may decide to abandon the website or firm.
This tutorial will help you create conditional statements in PHP, including how to use the if, else, and elseif keywords.
Step By Step Guide On PHP If Statement Multiple Conditions :-
PHP If Statement
First let's check out a normal If statement before checking php if statement multiple condition.
<?php $num=12; if($num<100){ echo "$num is less than 100"; } ?>
- The if statement in PHP allows code to be executed only if certain conditions are met. If the condition is true, it is executed.
- The if statement is used to execute the code block contained within it only if the provided condition is true.
PHP If-else Statement
<?php $num=12; if($num%2==0){ echo "$num is even number"; }else{ echo "$num is odd number"; } ?>
- PHP Whether the condition is true or false, the if-else expression is executed.
- The if-else statement differs slightly from the if statement. If the stated condition is true, it executes one piece of code; if the condition is false, it executes another block of code.
- We add an else block to the conditional if statement when we want to execute one piece of code over another. The if block's code will only be performed if the statement evaluates to true, but the otherwise block's code will only be executed if the statement does not evaluate to true.
PHP If-else-if Statement
<?php $marks=69; if ($marks<33){ echo "fail"; } else if ($marks>=34 && $marks<50) { echo "D grade"; } else if ($marks>=50 && $marks<65) { echo "C grade"; } else if ($marks>=65 && $marks<80) { echo "B grade"; } else if ($marks>=80 && $marks<90) { echo "A grade"; } else if ($marks>=90 && $marks<100) { echo "A+ grade"; } else { echo "Invalid input"; } ?>
- The if-else-if statement in PHP is a specific statement for combining several if?.else expressions. As a result, we can use this statement to verify numerous criteria.
PHP nested if Statement
<?php $age = 23; $nationality = "Indian"; //applying conditions on nationality and age if ($nationality == "Indian") { if ($age >= 18) { echo "Eligible to give vote"; } else { echo "Not eligible to give vote"; } } ?>
- The if block in the nested if statement is contained within another if block. The inner if statement is only executed if the outer if statement's condition is true.
Conclusion :-
Conditional statements allow us to regulate the flow of our programmes by determining the output options.
They are a fundamental building block of programming and may be found in almost any programming language.
It looked at nested statements and combining conditions and demonstrated the use of the if, else, and elseif keywords.
I hope this tutorial on PHP if statement multiple conditions helps you.