Condition works separately but not as OR - php

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/'))) {
...
}

Related

Execute on certain url format with strpos

I want to perform an action when the url is /bookings , but not /bookings/something-else
I tried this...
if ($_SERVER['REQUEST_URI'] == '/bookings') {
// do stuff....
}
But it fails when the user is on the /bookings page and searches, at which point queries are added to the url, e.g. /bookings?search=this
I have also tried this...
if (strpos($_SERVER['REQUEST_URI'],'/bookings') !== false && strpos($_SERVER['REQUEST_URI'],'/bookings/') == false ) {
// do stuff...
}
But this still executes on /bookings/some-thing and i cant figure out why?
You'll be better off using a dedicated method for URL parsing, rather than using string manipulation. PHP's parse_url function is perfect for this:
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($path === '/bookings') {
...
}
Try this condition. first will make exact string. while seocnd match with query sring
if ($_SERVER['REQUEST_URI'] == '/bookings' || strpos($_SERVER['REQUEST_URI'],'/bookings?') !== false) {
// do stuff....
}
OR use === while matching false/ otherwise 0 and false become equal
if (strpos($_SERVER['REQUEST_URI'],'/bookings') !== false && strpos($_SERVER['REQUEST_URI'],'/bookings/') === false ) {

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

IF statement with multiple conditions

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
}

How do I extract a single column from another database?

Why this condition passes even if I change the $_GET variable?
I've this code
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && $_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc') { /*rest goes here*/ } else {redirect}
Link returns like this
http://localhost/system/results.php?script_id=2&results=reorder&sort_column=supplier_address&sort_order=desc
But when I change this sort_column=supplier_address to say for example sorcodsalumn=supplier_address it doesn't redirect, instead goes ahead, any idea why? But if I simply remove few letters and dont replace with something else it does redirect...
How come if am using this isset($_GET['sort_column'] and am modifying sort_column to something else still passes this condition
Basic PHP operator precedence... && evaluates before ||, so your entire statement boils down to:
(x && y && z && ....) || ($_GET['sort_order'] == 'desc')
You need to simplify that if(), add some () to enforce your own evaluation order, and then things should start working a bit better.
your AND's and OR's need to be bracketed properly.
else if (isset($_GET['results']) &&
$_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) &&
$_GET['sort_column'] != '' &&
isset($_GET['sort_order']) &&
$_GET['sort_order'] != '' &&
($_GET['sort_order'] == 'asc' || $_GET['sort_order'] == 'desc'))
{
/*rest goes here*/
} else {
redirect
}
More specifically your last || needs its own brackets, as shown above.
You need to put a bracket around your || (OR) statement like this:
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && ($_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc')) { /*rest goes here*/ } else {redirect}
Otherwise your statement will return true anytime sort_order is set to 'desc'.

Categories