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.
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 == "" ) )
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")
I have some code that isn't working yet, before I debug I want to make sure that this syntax or method can indeed work and actually only execute the mysql_query if the last condition is true.
Also, is this a relatively safe practice?
I couldn't find anything relating to this, I figured someone putting it in English would help clear this up for me.
if($var1 == $var2) {$new = 1;}
if($vara == $varb) {$old = 1;}
if($new = 1 && $old = 1) { mysqli_query($somequery);}
This won't work because of the single =.
Go for:
if($var1 == $var2) {$new = 1;}
if($vara == $varb) {$old = 1;}
if($new == 1 && $old == 1) { mysqli_query($somequery);}
Or, ideally:
if ($var1 == $var2 && $vara == $varb) {
mysqli_query($somequery);
}
Top hint to stop things like if ($var = 1) typos - switch the comparisons around and put the constant first.
If you write if ($var = 1) then $var becomes 1 and is always true, but if you write if (1 = $var) you get an error, which is exactly what you want (and the same happens if your use a string if ("yes" = $var).
It been hammered into us to put the variable first since forever, but you're far better off doing it the other way around.
Simple but this has always bothered me. Which is the best way to condition statement?
$foo = '1';
if($foo === '1' || $foo === '2' || $foo === '3')
{
// foo matches
}
or
if($foo === '1' || '2' || '3')
{
// foo matches
}
Which step works and is better. is there a better solution?
The second version will always evaluate to true.
If you want to compact the comparison against multiple alternatives then use:
if (in_array($foo, array('1', '2', '3') )) {
If you want to closely match the exact comparison === then you would however need:
if (is_string($foo) && in_array($foo, array(...))) {
$foo = 1;
if(in_array($foo, array(1, 2, 3))){
//foo matches
}
This is an alternative:
if($foo>0 && $foo<4){
}
Second if statement won't work. PHP doesn't work like that. Any number other than 0 (including negatives) evaluates to true when alone in an if statement. This is why you can do something like if(count($array)) without specifying that count($array) must be greater than 0.
Would be the same as if you had said:
if($foo === 1)
{}
elseif(2) //This will always trigger if $foo !== 1.
{}
elseif(3) //This will never trigger because of the last one
{}
Each condition is it's own self contained condition. Instead of reading it as just "or" or "and" read it as "or if" and "and if". So if $foo is 1 or if 2 or if 3 instead of if $foo is 1 or 2 or 3
If it's just numeric, then amosrivera's solution is the best. If it's for other types of data, then webarto/mario have a good solution.