Php check if 5 variables are the same value - php

In php I need to check if 5 variables are the same value, and then proceed.
Instead of doing
if($a == 'logged' && $b == 'logged' && $c == 'logged' && $d == 'logged' && $e == 'logged')
{
// Code to run here
}
Is there a different way to do it? Like:
if($a,$b,$c,$d == 'logged')
Something like that possible..?

You can use something like this
$values = array($a, $b, $c, $d, 'logged');
if(count(array_unique($values)) === 1) {
//All elements are the same
}

I recommend against trying to shorten that. There is no more compact notation available to my knowledge apart from optical effects, none that the php parser accepts. The efficiency would not be better, since internally the same comparisons have to be done anyway.
Instead I usually try to enhance the readability of the code instead and use reversed notation, since human time is much more expensive than computer power these days :-)
if ( 'logged' == $a
&& 'logged' == $b
&& 'logged' == $c
&& 'logged' == $d
&& 'logged' == $e ) {
// Code
}

Related

How to write multiple variable if statement in php

So I have an if-statement and I am not sure how is the best way to write it. Or if there is a proper way of writing it. Can I just use && every time or should I separate them with more () or does it really matter, is there a difference in performance ect?
$a = 10;
$b = 20;
$c = 8;
$d = 25;
$e = "";
$f = "not blank";
// FIRST EXAMPLE
if (!empty($e) && !empty($f) && $a <= $c && $b <= $d)
{
// do something here
}
OR
if ( (!empty($e) && !empty($f)) && ($a <= $c && $b <= $d))
{
// do something here
}
It only depends of your needs. Don't use brackets when you don't need it. It making code harder to read. In your case you shouldn't use brackets, just:
if ( !empty($e) && !empty($f) && $a <= $c && $b <= $d)
{
// do something here
}
In this case there is completely no need to use more brackets because you have only &&. As it comes to performance, less brackets - less work for interpreter.
Generally whether you need to use brackets depends on the precedence of the operators you are using in the condition of the if statement. See the official PHP documentation on it for more detail.
Personally, in the case of if statements (and code in general within reason) I favour readability above all.
For instance, with the following if statement you suggested:
if (!empty($e) && !empty($f) && $a <= $c && $b <= $d)
{
// do something here
}
For someone who is not familiar with the code, it can initially take a bit longer to see what's happening. There are many ways to combat this. One example is splitting the condition into separate if statements and maybe throwing exceptions if it fits in with your logic. In this case your example could become something like this:
if (empty($e) || empty($f))
{
// throw exception as one or both of $e or $f is empty
}
if ($a <= $c && $b <= $d)
{
// do something here
}
The example may not be logically correct but hopefully it helps in throwing a different perspective on things.
You need to keep each statement in different brackets
if ( (!empty($e) && (!empty($f) && ($a <= $c) && ($b <= $d))
{
// do something here
}

PHP: Evaluate string as logical expression and return true or false

I have a txt file with hundreds of logical expressions.
I want to read each one (no problem so far) and to be able to evaluate it recursively, but I can't figure a way how. The expression has && and == and comparissons between strings and numbers. I don't want to use eval, as it's not recommended apparently and it didn't work in my case.
Example. Let's say I read these 2 strings:
s = "a == alpha && b == beta || b == omega", or
s = "g >= 2 && f != gamma"
I want to break them down to
($a == "alpha" && $b == "beta" || b == "omega")
($g >= 2 && f!= "gamma")
to use them in an if, so that it returns TRUE or FALSE. My problem is not with replacing the variables, it's with making them evaluate as a logical expression
Can anybody give me a hand?
Thanks in advance,
Cristina
Try this :
if( (($a == 'alpha' && $b == 'beta') || ($b == 'omega')) || ($g >= 2 && $f != 'gamma'))
{
// returns true
}
else
{
// returns false
}

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 == "" ) )

How do i make shortcuts for IF statements?

is there a shorthand version of checking if numerous variables have the same value please ?
instead of :-
if ($a="valid")
{do stuff;}
if ($b="valid")
{do stuff;}
if ($c="valid")
{do stuff;}
if ($d="valid")
{do stuff;}
is there something like:-
if ($a or $b or $c or $d = "valid")
{do stuff;}
Just put all of the variables you want to check into an array and use in_array() to check them all at once:
if (in_array('valid', array($a, $b, $c, $d))) {
do stuff;
}
John Conde's solution of putting together an array and then using in_array is a good solution. In case you want to stick with basic string comparisons, somewhat like in your example code, then you could do:
if ($a == 'valid' or
$b == 'valid' or
$c == 'valid' or
$d == 'valid') {
// DO STUFF
}
Something to keep in mind, based on what I noticed from your example code: When doing comparisons for equality in PHP, use == and not =. The single = is for assignment, while the double == is for comparison.
You could extract a function doing the comparison and have :
if (check($a) || check($b) ...)
It's not as sexy as John proposition (whatever his girlfriend says) I have to admit.
if ($a == 'valid' || $b == 'valid' || $c == 'valid' || $d == 'valid') { // DO STUFF }
This would be best because if first condition is true then no need to check rest.
Yes you can, like this:
if (($a || $b || $c || $d) == "valid")

php if statement with one of two conditions if( a < b) or (b=0)

I have some huge blocks of code and I would like to avoid using elseif so the question is : Is it possible to construct an IF function with two possibilities in the same statement ?
something like if( a < b) or (b=0)
{
statement
}
if( ($a < $b) || ($b==0) )
{
//do something
}
or even better
if( ($a < $b) || (0==$b) )
{
//do something
}
so you don't accidentally assign 0 to $b.
if( ($a<$b) OR ($b == 0) )
{
//do something
}
Parenthesis in this case are not necessary, just added for clarity.
($a < $b ? 'a is smaller' : 'a equals or is greater');
Quick and easy, but not easily to maintain (personal opinion).

Categories