PHP If ORPHP If condition can be compound condition. So, we can join multiple simple conditions with logical OR operator and use it as condition for PHP If statement. In this tutorial, we will go through the syntax of using OR operator in If-statement and some examples to understand the usage.
Syntax – PHP If with OR operatorThe syntax of If-statement with OR logical operator is if ( condition_1 || condition_2 ) {
//if-block statement(s)
} where - condition_1 and condition_2 can be simple conditional expressions or compound conditional expressions.
|| is the logical OR operator in PHP. It takes two operands: condition_1 and condition_2.
Since we are using OR operator to combine the condition, PHP executes if-block if at least one of the condition_1 and condition_2 is true. If both the conditions are false, then PHP does not execute if-block statement(s). Example – PHP If with OR OperatorIn this example, we will write an if statement with compound condition. The compound condition contains two simple conditions and these are joined by OR logical operator. PHP Program <?php
$a = 2;
$b = 8;
if ( ( $a == 2 ) || ( $b == 5 ) ) {
echo "a is 2 or b is 5.";
}
?> Output
ConclusionIn this PHP Tutorial, we learned how
to write PHP If statement with AND logical operator. (PHP 4, PHP 5, PHP 7, PHP 8) The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C: As described in the section about expressions, expression is evaluated to its Boolean
value. If expression evaluates to true , PHP will execute statement, and if it evaluates to false - it'll ignore it. More information about what values evaluate to false can be found in the 'Converting to boolean' section. The following example would display a is bigger than
b if $a is bigger than $b:
<?php if ($a > $b) echo "a is bigger than b"; ?>
Often you'd want to have more than one statement to be executed conditionally. Of course, there's no need to wrap each statement with an if clause. Instead, you can group several statements into a statement group. For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:
<?php if ($a > $b) { echo "a is bigger than b"; $b = $a; } ?>
If statements can be nested infinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program. robk ¶ 9
years ago
easy way to execute conditional html / javascript / css / other language code with php if else:
<?php if (condition): ?>
html code to run if condition is true
<?php else: ?>
html code to run if condition is false
<?php endif ?>
techguy14 at gmail dot com ¶ 11 years ago
You can have 'nested' if statements withing a single if statement, using additional parenthesis.
For example, instead of having:
<?php
if( $a == 1 || $a == 2 ) {
if( $b == 3 || $b == 4 ) {
if( $c == 5 || $ d == 6 ) {
//Do something here.
}
}
}
?>
You could just simply do this:
<?php
if( ($a==1 || $a==2) && ($b==3 || $b==4) && ($c==5 || $c==6) ) {
//do that something here.
}
?>
Hope this helps!
Christian L. ¶ 11 years ago
An other way for controls is the ternary operator (see Comparison Operators) that can be used as follows:
<?php $v = 1;$r = (1 == $v) ? 'Yes' : 'No'; // $r is set to 'Yes' $r = (3 == $v) ? 'Yes' : 'No'; // $r is set to 'No'echo (1 == $v) ? 'Yes' : 'No'; // 'Yes' will be printed
// and since PHP 5.3
$v = 'My Value'; $r = ($v) ?: 'No Value'; // $r is set to 'My Value' because $v is evaluated to TRUE$v = ''; echo ($v) ?: 'No Value'; // 'No Value' will be printed because $v is evaluated to FALSE ?>
Parentheses can be left out in all examples above.
grawity at gmail dot
com ¶ 14 years ago
re: #80305
Again useful for newbies:
if you need to compare a variable with a value, instead of doing
<?php if ($foo == 3) bar(); ?>
do
<?php if (3 == $foo) bar(); ?>
this way, if you forget a =, it will become
<?php if (3 = $foo) bar(); ?>
and PHP will report an error.
cole dot trumbo at nospamthnx dot gmail dot com ¶ 5
years ago
Any variables defined inside the if block will be available outside the block. Remember that the if doesn't have its own scope.
<?php $bool = true; if ($bool) { $hi = 'Hello to all people!'; } echo $hi; ?>
It will print 'Hello to all people!'
On the other hand, this will have no output:
<?php if (false) { $hi = 'Hello to all people!'; } echo $hi; ?>
Donny Nyamweya ¶ 11 years ago
In addition to the traditional syntax for if (condition) action; I am fond of the ternary operator that does the same thing, but with fewer words and code to type:
(condition ? action_if_true: action_if_false;)
example
(x > y? 'Passed the test' : 'Failed the test')
How do you or condition in if in PHP?
Syntax – PHP If with OR operator
Since we are using OR operator to combine the condition, PHP executes if-block if at least one of the condition_1 and condition_2 is true. If both the conditions are false, then PHP does not execute if-block statement(s).
What are the 4 conditional statements in PHP?
PHP Conditional Statements
if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions.
What is a nested IF statement in PHP 8?
Nested if Statement
We can make nesting statement by nesting the if statement. It means when we insert a second if statement inside an if statement, we call it nested if statement or nesting the if statement.
What does mean in PHP?
The spaceship operator <=> is the latest comparison operator added in PHP 7. It is a non-associative binary operator with the same precedence as equality operators ( == , !=
|