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")
Related
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
}
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 == "" ) )
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
}
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).
Is it possible to do something like this in a php if statement:
if($a == '1' || ($b == '2' && $c == '3')) echo "foo walks into a bar";
(also, is the title of my question phrased correctly?)
Yes, as $a == '1' || ($b == '2' && $c == '3') evaluates into a Boolean expression. Think of || and && as mathematical operators, and you can apply brackets to them to alter their order of operations.
Of course you can.
http://www.php.net/manual/en/language.operators.precedence.php
Yes
php.net/if
Think you might need to bracket up the first condition:
if(($a == '1') || ($b == '2' && $c == '3')) echo "foo walks into a bar";
They absolutely can! I think it'd be called something like a "multiple condition if()" statement.