I am surprised with the output of OR operator in php
$a = 5;
echo $b = ((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
It echo 5 but i expect NULL
It should be like this.
$a = 5;
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
As you stated, your $a is not 8 or 2. So assume it's 5.
How your evaluation works:
((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
Compare intval($a) == 8.
No matter what you get from #1, do || 2 which leads to true (non-zero number is loosely equals true)
Compare intval($a) != 0 - leads to true.
Compare true && true => true
Answer is $a.
Step-by-step:
(intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL;
(false || 2) && intval($a) != 0 ? $a : NULL);
(false || true) && intval($a) != 0 ? $a : NULL;
true && intval($a) != 0 ? $a : NULL;
true && true ? $a : NULL;
$a;
TL;DR
To get NULL, change || 2 to || intval($a) == 2
change this
intval($a) == 8 || 2
to
intval($a) == 8 || intval($a) == 2
i.e
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
The way you write comparison for OR is not possible in PHP. Use below code.
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
Related
This seems pretty simple, but I can't figure that out.
I am checking a few variables before showing some stuff on the screen, and I have a variable which can be null at some point.
On my if statement I have:
if ($a != 'abc' && ($a == $b || $b == $c) && min($variable) > 3) { ... }
How can I set it true if min($variable) is null, if all the other statements are true?
As I understood from your question,
the min($variable) maybe null and you want to check it whether it's value less than 3 or null value ?
if ($a != 'abc' && ($a == $b || $b == $c)
&& (min($variable) > 3 || min($variable) == null)) { ... }
could be using a ternary operator inside ( )
if ($1 != 'abc' && ($1 == $2 || $2 == $3) &&
( $variable == null ? true : min($variable) > 3) ) { ... }
I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..
I basically have this PHP code:
<?php
$num_1 = $_POST['num_1'];
if( $num_1 == 1 || $num_1 == 2 ){
// Do something
}
?>
What I know is that $num_1 can either be 1 or 2. This is the value I expect from $_POST['num_1']
What I want to know is that instead of using if( $num_1 == 1 || $num_1 == 2 ) could I use or is that the same to use:
<?php
$num_1 = $_POST['num_1'];
if( $num_1 == ( 1 || 2 ) ){
// Do something
}
?>
The difference here is that previously I was using if( $num_1 == 1 || $num_1 == 2 ){} but now I am using if( $num_1 == ( 1 || 2 ) ){}
I am trying to wrap my head around this. Could anyone provide a helpful explanation? Which is more efficient and best practice?
Not the same. You can test it just by doing something like that:
$num_1 = 1;
var_dump($num_1 == ( 1 || 2 )); //true
var_dump($num_1 == 1 || $num_1 == 2); //true
$num_1 = 2;
var_dump($num_1 == ( 1 || 3 )); //true
var_dump($num_1 == 1 || $num_1 == 3); //false
The main reason for this is that when you are trying to do this:
if(1 || 2)
PHP will convert integers to booleans. So, your expresion become:
if(true||true) //what is actually true
Am I correct in thinking that the below statement will validate if $a is either '1' or '0', and $b is either 'x' or 'y'
if ($a == '1' && $b == 'x' || $b == 'y')
or do I have to be more specific, i.e.
if ($a == '1' && $b == 'x' || $a == '1' && $b == 'y')
You need to use parenthesis to specify precedence:
if ($a == '1' && ($b == 'x' || $b == 'y'))
if (($a == '1' && $b == 'x') || ($a == '1' && $b == 'y'))
You need this:
if (($a == '1') && ($b == 'x' || $b == 'y'))
The first would be sufficent for that, but you have to put a parenthesis around $b:
if ($a == '1' && ($b == 'x' || $b == 'y'))
I have the following test on some data:
if(isset($option_type)
&& ($option_type == 3 || $option_type == 4
&& (isset($ext_data) && $ext_data != "")
)
){}
What it should do, is if $option type is NOT 3 or 4, the test should return true. If $option type is 3 or 4 AND $ext_data does not exist or equals "", then the test should return false.
However, no matter what $option_type is, it returns false.
How can I format this test so when the $option_type is 3 or 4 and $ext_data exists and is not "", the result is true?
lee
I would split the tests into two to make it clearer:
if (!isset($ext_data) || $ext_data == "") return false;
return $option_type != 3 && $option_type != 4;
//try this
if(isset($option_type)&& ($option_type == 3 || $option_type == 4)){
if (isset($ext_data) && $ext_data == ""){
return false;
}else{
return true;
}
}