This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
The first one with equals will work and I would expect that. But the second is not working as expected.
$nicktest = "new";
if ($nicktest == "new" || $nicktest == "fun" || $nicktest == "norm" || $nicktest == "pvp")
{
$odpoved = "Ok it works.";
echo $odpoved;
return;
}
$nicktest = "new";
if ($nicktest != "new" || $nicktest != "fun" || $nicktest != "norm" || $nicktest != "pvp")
{
$odpoved = "Ok it works too why?";
echo $odpoved;
return;
}
Your 4 conditions get the following evaluation:
false, true, true, true
Now,
if (false || true || true || true) {
// Code block is actually evaluated
}
If you would happen to have:
if (false || false || false || false) {
// Code block wouldn't be evaluated
}
Under this scheme, at least one true condition would get the code to be evaluated
Related
This question already has answers here:
whats the difference in parentheses in IF statements?
(9 answers)
Closed 2 years ago.
I have this simple if statement but I do not get the results I expect. If all three vars match then I get not supported as expected. However I expect that as soon as I change one of the vars to a value that is not in the if statement, e.g. $Main = "SomethingElse", for the if statement to not match and therefor echo supported. However, supported is only returned if all three vars do not match the if statement.
Why does this happen?
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && $Backup == "Back_Other" || $Backup == "Back_None") {
echo "not support";
} else {
echo "supported";
}
In your example the if statement will always return true if the value of $backup is set to Back_None.
Try using below code. Here it will check $backup value first using || operator and then it will check the result with && operator
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && ($Backup == "Back_Other" || $Backup == "Back_None")) {
echo "not support";
} else {
echo "supported";
}
I have been making stuff in PHP for a while, everything is always fine. But today I don't get this statement. Why is it always true?
if ($action != 1 || $action != 2) echo true; // TRUE for 0, 1, 2, 3
But the reverse logic
if ($action == 1 || $action == 2) echo true; // FALSE for 0, 3 TRUE for 1, 2
The first expression blows my mind. I guess I don't understand something very very basic, not in PHP but in the Universe, so I don't get it here. I thought that if (FALSE || TRUE) == FALSE, but it isn't a case for second example. It works as expected.
So, where is the answer how to say that: "If the variable is not 1 OR 2 - echo true". I don't understand why my if ($var != 1 OR $var != 2) echo true; doesn't work as I expect.
Negation of ($action != 1 || $action != 2) is ($action == 1 && $action == 2). You can see for yourself that the latter is always false because variable can not be both 1 and 2 at the same time. Therefore the original condition is bound to be always true.
It is working as it has to work. See the doc http://php.net/manual/en/language.operators.logical.php
$a || $b returns TRUE if either $a or $b is TRUE.
If you try like this, hope it will make sense, see the comment on every line
$action = 0;
var_dump($action != 1 || $action != 2); //here (true || true)
$action = 1;
var_dump($action != 1 || $action != 2); //here (false || true)
$action = 2;
var_dump($action != 1 || $action != 2); //here (true || false)
$action = 3;
var_dump($action != 1 || $action != 2); //here (true || true)
Condition OR will search for first TRUE result, your code will give TRUE all the time as any value will be even not 1 or 2.
From you example if $action = 1 then the condition $action != 2 will give true, also if $action = 2 then $action != 1 will give TRUE.
For (If the variable is not 1 OR 2 - echo true) use this:
if(!in_array($action, array(1,2)) echo "true";
EDIT:
You can also check it like this:
if(!($action == 1 || $action == 2)) echo "true";
if ($action == 2 || $action == 1) echo true; // FALSE for 0, 3 TRUE for 1, 2
Works as intended. Its basically "echo true if $action is either 1 or 2".
I have a problem with a form check that use an if statement with multiple 'and' and 'or' operators. This check return me an anomalous occasionally false value.
public function insert_checkForm($form) {
$form = array_filter($form);
if (
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
isset($form['travel_go_from']) != isset($form['travel_go_to']) ||
isset($form['work_go_from']) != isset($form['work_go_to']) ||
!isset($form['travel_go_from']) &&
!isset($form['travel_go_to']) &&
!isset($form['work_go_from']) &&
!isset($form['work_go_to'])
) {
return false;
} else {
return $form;
}
}
Last question, the above code changes compared to this (in spite of the priorities of and operators)?
[...]
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
(isset($form['travel_go_from']) != isset($form['travel_go_to'])) ||
(isset($form['work_go_from']) != isset($form['work_go_to'])) ||
(!isset($form['travel_go_from']) && !isset($form['travel_go_to']) && !isset($form['work_go_from']) && !isset($form['work_go_to']))
[...]
Thanks =)
The most common problem with isset() is that it returns false when the item is NOT SET but also returns false if the item IS SET && IS NULL.
isset($arr['nonexisting']); //this returns: false
$arr['existing'] = null;
isset($arr['existing']); //this returns: false
I'm sorry for the vaguely described title. This is what I want:
if($a[$f] === false || $a[$g] === false || $a[$h] === false || $a[$i] === false || $a[$j] === false)
{
// do something
}
I want to do something with the condition that actually triggered the statement (if a[$f] = true and a[$g] = false, I want to do something with $g).
I know that in this case, the first statement that went true (i.e. $a[$g] == false) triggers. But is there any way to do something with $g? I've never seen this in my programming life before and can't seem to find anything about it.
Thanks in advance.
--- Edit ---
I forgot to mention: I'm using a function on all the array data. So, shortened, I get this:
if(valid($a[$f]) === false || valid($a[$g]) === false)
{
// do something
}
--- Edit 2 ---
This piece of OOP-based PHP, where I'm in a class, is my code.
if($this->validatedText($product[$iName]) == false ||
$this->validatedUrl($product[$iUrl]) == false ||
$this->validatedNumber($product[$iTax]) == false ||
$this->validatedValuta($product[$iPrice]) == false ||
$this->validatedText($product[$iArticleNumber]) == false ||
$this->validatedText($product[$iDescription]) == false ||
$this->validatedText($product[$iMetaDescription]) == false ||
$this->validatedText($product[$iTitle]) == false)
{
// do something with the first iVariable
}
Simplest solution will be
if(false!==($sIndex = array_search(false, $a, 1)))
{
//your $sIndex is first index with false value
}
if you want all keys, you may use array_filter(), like this:
$rgFalse = array_keys(array_filter($a, function($x)
{
//here valid is your function
return false===valid($x);
}));
I can’t get the desired result from a series of conditions in an IF.
if (($varteam == $_POST['rteam1']) && ($varteam == $_POST['rteam2']) && ($varteam == $_POST['rteam3']) && ($varteam == $_POST['rteam4']) && ($varteam == $_POST['rteam5']))
{true}
else
{false}
Starting from the variable $varteam I want to obtain true if all the compared values are identical, otherwise false.
The compared values may also be null.
With the code I’ve posted it works if all the values are equal or different but I get true instead of false if one or more values are different.
Why does it happen?
I am guessing that you may get false positives when you have 0 mixed with null or false. Just to be on the safe side, use === instead of == so type checking is in effect. That way, null !== false !== 0.
if (($varteam === $_POST['rteam1']) &&
($varteam === $_POST['rteam2']) &&
($varteam === $_POST['rteam3']) &&
($varteam === $_POST['rteam4']) &&
($varteam === $_POST['rteam5']))
{
// true
}
else
{
// false
}