IF statement with multiple conditions - php

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
}

Related

Condition works separately but not as OR

I have two conditional statements that are the following :
if( isset( $query_string['page'] ) && strpos($_SERVER['REQUEST_URI'], '/blog/') !== false && strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false ) {
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && strpos($_SERVER['REQUEST_URI'], '/blog/') !== false && strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false ) {
The last condition in both if statements is :
strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false
I would like to change the last condition for both, which would be in plain English :
If all criteria are matched and the URL contains either '/blog/page/' or '/blog/tag/' do something.
When I interchange the last condition from '/blog/page/' to '/blog/tag/' the code works. As soon as I try to have both at the same time, the code doesn't work anymore.
I've tried to change the && to and and use || for the or condition, in order to keep the right precedence. I have tried to put them between parenthesis in order to handle the precedence, none of them worked.
I even tried :
strpos($_SERVER['REQUEST_URI'], '/blog/page/') || strpos($_SERVER['REQUEST_URI'], '/blog/tag/') === false
Which didn't help either.
<?php
// Your code says "=== false" (doesn't match)
// but your English description says "contains either '/blog/page/' or '/blog/tag/'" (match)
// This assumes you want what your English description says
/**
* Returns a boolean indicating if the given URI part is found
*/
function match($uriPart)
{
return strpos($_SERVER['REQUEST_URI'], $uriPart) !== false;
}
/**
* Returns a boolean indicating if the given URI part is not found
*/
function doesNotMatch($uriPart)
{
return strpos($_SERVER['REQUEST_URI'], $uriPart) === false;
}
// In this case, "match('/blog/')" is redundant because you're checking for other strings which contain it.
// Nevertheless, I'm leaving it as-is.
if( isset( $query_string['page'] ) && match('/blog/') && (match('/blog/page/') || match('/blog/tag/'))) {
...
// In this case, "match('/blog/')" is redundant because you're checking for other strings which contain it.
// Nevertheless, I'm leaving it as-is.
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && match('/blog/') && (match('/blog/page/') || match('/blog/tag/'))) {
...
}

PHP - An if statement with concatenated '&&' and '||' can give problems?

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

PHP Possible ways to write an if

I'm wondering if there is any way to make this code shorter. I'm using 2 if statements and I'm looking to only use one. The things is $user is the session and if you check if $user->userId exists on the same line, the code will error when no session exists. Caused by requesting the userId from an object that does not exist. That's pretty logical but now is there any solution?
if ($user != null) {
if ($user->userId == 1) {
..
}
}
How about using the && operator:
if ($user && $user->userId == 1) {
//...
}
You can add as many sentences as you want, as long as they are properly built, in this case:
if (($user != null) && ($user->userId == 1)) {
or you could simply:
if ($user && ($user->userId == 1)) {
if ($user) just checks if the variable is set, or if it is not null.
You want to use the && operator. It means and
if ($user && $user->userId == 1) {
// do some things
}
You may also want to look into the || operator, it means or.
The && operator will return true ONLY if the two predicates return true.
The || operator will return true as long as one of the predicates return true.

PHP: If-statement, need value of condition that triggered

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);
}));

Please explain this php expression "!$variable"

What does an exclamaton mark in front of a variable mean? And how is it being used in this piece of code?
EDIT: From the answers so far I suspect that I also should mention that this code is in a function where one of the parameters is $mytype ....would this be a way of checking if $mytype was passed? - Thanks to all of the responders so far.
$myclass = null;
if ($mytype == null && ($PAGE->pagetype <> 'site-index' && $PAGE->pagetype <>'admin-index')) {
return $myclass;
}
elseif ($mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) {
$myclass = ' active_tree_node';
return $myclass;
}
elseif (!$mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) {
return $myclass;
}`
The exclamation mark in PHP means not:
if($var)
means if $var is not null or zero or false while
if(!$var)
means if $var IS null or zero or false.
Think of it as a query along the lines of:
select someColumn where id = 3
and
select someColumn where id != 3
! negates the value of whatever it's put in front of. So the code you've posted checks to see if the negated value of $mytype is == to null.
return true; //true
return !true; //false
return false; //false
return !false; //true
return (4 > 10); //false
return !(4 < 10); //true
return true == false; //false
return !true == false; //true
return true XOR true; //false
return !true XOR true; //true
return !true XOR true; //false
! before variable negates it's value
this statement is actually same as !$mytype since FALSE == NULL
!$mytype == null
statement will return TRUE if $mytype contains one of these:
TRUE
number other than zero
non-empty string
elseif (!$mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) {
return $myclass;
}
The above !$mytype == null is so wrong. !$mytype means that if the variable evaluates to false or is null, then the condition will execute.
However the extra == null is unnecessary and is basically saying if (false == null) or if (null == null)
!$variable used with If condition to check variable is Null or Not
For example
if(!$var)
means if $var IS null.

Categories