Shorthand expression for an if ( $a == $b || $a == $c ) statement - php

I know this code will work:
echo ( $a == $b || $a == $c ) ? "Yes" : "No";
That can be read like:
if $a is equal to $b or $a is equal to $c
Is there a way to make it more shorter like:
if $a is equal to $b or $c
I have tried a lot including this but still no luck:
echo ( $a == ( $b xor $c ) ) ? "Yes" : "No";

You can use in_array:
var_dump(in_array($a, [$b, $c]));
with your example:
echo in_array($a, [$b, $c]) ? 'Yes' : 'No';
Note: this syntax is only useful if you have more than 2 values. For few values $a == $b || $a == $c does the job well and is probably faster.

These are two alternatives, but they will both take longer to execute than the code you posted because they rely on more complex functions.
preg_match('/^('.$b.'|'.$c.')$/',$a) === 0
in_array($a,array($b,$c)) === true
If you put the condition more likely to be true as the first expression, in most cases, PHP will evaluate the expression as true and not test the second expression.

Related

Shorthand expression for an if ( $a == $b and $a == $c ) statement in PHP

Recently, I posted a question about if ( $a == $b or $a == $c ) expression. Now I want to know if there's also a shorthand expression for this code:
if ( $a == $b && $a == $c ) { /*statement*/ }
That code can be read like "if $a is equal to $b and $a is equal to $c".
Is there code that's more shorter that can be read like:
if ( $a is equal to ( $b and $c ) ) { /*statement*/ }
Because you are testing if $a is equal to $b and $c this logically results in $b being equal to $c, I don't know how much shorter this would be but if you wanted to quickly test if all prepossed values are the same you could create an array of them and call array_unique. If the length of the resulting array is 1 then they are all equal to each other. I imagine this would only be easier if you had a large set of values, as the syntax might obscure your intent.
I found an answer upon experimenting while my question is not yet posted:
if ( $a == ( $b & $c ) ) { /*statement*/ }
I just want to share it.. (And no, I'm developing a WordPress theme and this is unintentional!)

How do I use or operator in PHP?

if($a=="" and $b=="" or $c=="") {
echo "something";
}
I want to ask that if $a AND $b is empty then it will print: something. But if $b is not empty, then it will check the statement like this e.g. if $a is empty AND $c is empty then print: something (Means $a is compulsory to check with the variable which is empty $b OR $c)
See the PHP Operator Precedence table. and has higher precedence than or, so your condition is treated as if you'd written
if (($a == "" and $b == "") or ($c == ""))
Since you want the $a check to be independent, you need to use parentheses to force different grouping:
if ($a == "" and ($b == "" or $c == ""))
Note that in expressions, it's conventional to use && and || rather than and and or -- the latter are usually used in assignments as a control structure, because they have lower precedence than assignments. E.g.
$result = mysqli_query($conn, $sql) or die (mysqli_error($conn));
See 'AND' vs '&&' as operator
I suppose you need something like this:
if (
($a == '')
&&
(
($b == '')
||
($c == '')
)
)
so, it will print something only when $a is empty and either $b or $c empty
Just force the comparison order/precedence by using parentheses:
if( $a == "" and ( $b == "" or $c == "" ) )
But you would be better off using && and ||:
if( $a == "" && ( $b == "" || $c == "" ) )

PHP OR surprising behavior [duplicate]

This question already has answers here:
The behaviour of the or operator in PHP
(4 answers)
Closed 8 years ago.
I have some surprising results using OR as a logical OR in php.
Considering the following code:
$a = false;
$b = false;
$c = true;
# Test 1, using OR and assigning to a variable
$value = $a OR $b OR $c;
var_dump( $value );
# return bool(false) but why?
# Test 2, using OR directly in var_dump
var_dump( $a OR $b OR $c );
# return bool(true) as expected
# Test 3, using || and assigning to a variable
$value = $a || $b || $c;
var_dump( $value );
# return bool(true) as expected
# Test 4, using || directly in var_dump
var_dump( $a || $b || $c );
# return bool(true) as expected
Why Test 1 and Test 2 give different results even though they do the same logical operation?
The || operator and OR operator do not behave the same. They cannot be used interchangably.
If you want || behaviour, then use it. Do not use OR unless you're in a situation where || would do the wrong thing.
As for your situation, these two lines of code will behave exactly the same:
$value = $a OR $b OR $c;
($value = $a) OR $b OR $c;
In other words, your code is basically just:
$value = $a;
If you used the || operator, then these two are identical as if you had braces like this:
$value = $a || $b || $c;
$value = ($a || $b || $c);
For more details: http://php.net/manual/en/language.operators.precedence.php
If you wrap test 1 in parenthesis, it will behave as expected:
$value = ($a OR $b OR $c);
When you run var_dump on test 2, you get the expected result because var_dump is wrapping the operation in parenthesis.
It is usually a good idea to wrap an operation in parenthesis like this, especially with variable assignment.
Also, the "OR" keyword and "||" do not behave the same way. See documentation here:
http://php.net/manual/en/language.operators.logical.php

"AND &" operator work fine but "OR |" and Either-OR "^" always true

I am using logical operators to test variables but AND & operator work fine but OR | and Either-OR ^ always true.
Why?
$a = 6;
$b = 6;
if ($a OR $b == 3) {
echo 'true <br />';
}
else {
echo 'false <br />';
}
The issue is with your syntax.
You need to look at the expression separately.
if($a) OR if($b == 3)
is what you're doing.
What you want is:
if($a == 3 || $b == 3)
If you look at $a by itself, any value except for 0 will return true making the entire equation true thanks to the OR
Because you have to OR to Boolean results - you're reading it too much like English.
if ($a == 3 || $b == 3)
rather than
if ($a OR $b == 3)
It's a matter of precedence - see http://php.net/manual/en/language.operators.precedence.php for more details here.
Both the other answers give you the code you need.
$a = true
$b = true
if($a and $b) TRUE if both $a and $b are TRUE.
Reference: http://php.net/manual/en/language.operators.logical.php

Can the || operator be used to select between values of non-boolean types in PHP?

Is it possible to echo using ||, so that it uses the first variable that evaluates to true?
for example,
$a = false;
$b = 'b';
echo $a || $b || 'neither'; // evaluates to 1 ?
Ternary operator
echo (($a) ? : $b) ? : 'neither';
Ultimate ternary
$a = false;
$b = 'b';
echo ($a)?$a:(($b)?$b:'neither');
echo $a ? $a : ($b ? $b : ($c ? $c : 'neither'));
and You go on like this if have more variables but it will get ugly and hard to read when too long.

Categories