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) ) { ... }
Related
I am attempting to ask:
If my variable $a is set
And if my variable $a is not apple or orange
Then do something
I'm attempting to do this with the following code, but true is returned.
Any ideas what I am doing wrong?
<?php
$a = 'banana';
if (isset($a) && ($a !== 'apple' | $a !== 'orange')) {
echo 'true';
} else {
echo 'false';
}
You probably meant && instead off || (not |):
And if my variable $a is not apple or orange
So if you don't want it to be either of those values, you'll need
($a !== 'apple' && $a !== 'orange')
So the script becomes
<?php
$a = 'banana';
if (isset($a) && $a !== 'apple' && $a !== 'orange') {
echo 'true';
} else {
echo 'false';
}
Now, false is shown if:
$a is not set
$a === apple
$a === orange
Try it online!
You're missing a | in your or expression
if (isset($a) && ($a !== 'apple' || $a !== 'orange')) {
echo 'true';
} else {
echo 'false';
}
You are missing a | OR operation is ||
if (isset($a) && ($a !== 'apple' || $a !== 'orange')) {
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 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);
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'))
<?php
$gender = 0;
if (($gender != 0) || ($gender != 1))
{
die('error:Must select a gender.');
}
?>
This should give a error if the gender is anything other than 1 or 0. So if i gave 5 it should die. If i gave it 1 it should not die. If i give it 0 it should not die.
I was thinking about a few work arounds
<?php
$gender = 0;
if ($gender == 0)
{
//number is okay
}
else if ($gender == 1)
{
//number is okay
}
else
{
die('error:Must select a gender.');
}
?>
Well that looks sloppy and it would work or i could create a array with 0 and 1 and check if its in it or not. Kinda overkill i think.
Not sure what i'm doing wrong.
You're using the wrong boolean operator:
if (($gender != 0) || ($gender != 1)) {
}
This will be entered if gender is 0, too, because it isn't 1, and vice versa. What you need to do is:
if (($gender != 0) && ($gender != 1)) {
}
Look at this table:
gender A (gender != 0) B (gender != 1) A || B A && B
----------------------------------------------------------------
0 false true true false
1 true false true false
5 true true true true
Also note Joshua's suggestion in the comments:
if (($gender == 0) || ($gender == 1)) {
/* number is ok */
} else {
die();
}
which is a bit longer, but more readable.
if (($gender != 0) && ($gender != 1))
^^
You want to die if both tests are true.
Change the or (||) to and (&&) and it should work, as you only want to fail, when it’s both not 0 and not 1.