php: I need the OPPOSITE of this please...? - php

The following searches the string $fruit for any of the words apples, oranges and bananas and acts accordingly:
if ((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false) {//some code }
I need the OPPOSITE. I need code to run if the string $fruit does NOT have any of the three. I was thinking something like
if not ((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false) {//some code }
but that doesnt seem to work...
A little hand holding please? Thanks...

What you're looking for is DeMorgan's law:
The negation of a conjunction is the disjunction of the negations.
The negation of a disjunction is the conjunction of the negations.
Try:
if ( stristr($fruit, 'apples') === FALSE && stristr($fruit,'oranges') === FALSE && stristr($fruit,'bananas') === FALSE) {
// some code
}
Since stristr only returns a string or FALSE, this code can be simplifed to:
if ( !stristr($fruit, 'apples') && !stristr($fruit,'oranges') && !stristr($fruit,'bananas')) {
// some code
}

Thanks for the help everyone.
I ending up doing this, based on all input from those that replied, which worked:
if (!((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false))
Again, thanks for the help!

if (!(stristr($fruit,'apples')) || !(stristr($fruit,'oranges')) || !(stristr($fruit,'bananas')) !== false) {//some code }

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

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 ) {

If statement behaves strange

I have 2 URLs say
http://localhost/xyz?language=en
http://localhost/xyz?language=es
for which I want to check if language parameter has something other than en/es, then it should redirect to some http://localhost/xyz/errorpage
For this I have below code:
if(isset($_GET['language'])){
if(($_GET['language'] !== "en") || ($_GET['language'] !== "es")){
header('Location: /xyz/errorpage');
}
}
But practically when I execute the any of the 2 URLs or putting value of language parameter to something different than en/es:
http://localhost/xyz?language=en
http://localhost/xyz?language=es
http://localhost/xyz?language=esdfsdf
I am redirected to errorpage
Cannot understand the issue with code.
Replace || by &&.
The reason :
You want to redirect only if this is not en AND not es.
change the if statment to && instead of || or your condition will be always false.
if(isset($_GET['language'])){
if($_GET['language'] !== "en" && $_GET['language'] !== "es"){
header('Location: /xyz/errorpage');
}
}
You have bad condition, or better, operator.
Use && instead of ||, or in_array().
if(($_GET['language'] !== "en") && ($_GET['language'] !== "es")) {
Using in_array() function:
if (!in_array($_GET['languge'], array('en', 'es'))) {
header ();
}
Condition if ($a != 'x' || $a != 'y') is always true, first or the second part of condition has be true. There are no other ways.

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

PHP problem, I'm sure this has an easy solution, just can't see it

I am new to php and this little bugger has been eating up my day, perhaps it's due to some property of php I am unaware of?
As part of some code for getting some data out of an xml file (using the event based Expat parser), I have the following code
$xmlFields;
$fieldName = "";
............... some other code ............
function char($parser,$data)
{
global $xmlFields, $fieldName;
if($fieldName) {
if($fieldName == "brandName" || "oeNumber" || "articleId" || "quantityPerPackingUnit" || "attrName") {
$xmlFields[$fieldName] = $data;
echo $data;
}
}
}
I try to echo $xmlFields["brandName"] for example, and nothing is printed.
1) I know that $xmlFields["brandName"] is non-empty because echo $data actually returns something.
2) If I change to $xmlFields[$fieldName] = 'some string';
then echo $xmlFields["brandName"] will print 'some string'
so why won't it print $xmlFields["brandName"]?
Thanks in advance,
Yazan
You cannot link ORs like that.
try
if($fieldName == "brandName" || $fieldName =="oeNumber" || $fieldName =="articleId" || $fieldName =="quantityPerPackingUnit" || $fieldName == "attrName") {
As Deceze said a much better option when you are searching in an array is to use
if (in_array($fieldName, array("brandName", "oeNumber", "articleId", "quantityPerPackingUnit", "attrName")))
I know some languages allow such construct but php is not one of them.
The following expression
$fieldName == "brandName" || "oeNumber" || "articleId" || "quantityPerPackingUnit" || "attrName"
is parsed as
(
(
(
($fieldName == "brandName") || ("oeNumber")
) || ("articleId")
) || ("quantityPerPackingUnit")
) || ("attrName")
Notice that your equality check is separated from the other checks. In this case, the expression always evaluates to true.
You can use an array for this case:
in_array($fieldName, array("brandName", "oeNumber", "articleId", "quantityPerPackingUnit", "attrName"));
Try this as a shorter version of Iznogood's answer:
if (in_array($fieldName, array("brandName", "oeNumber", "articleId", "quantityPerPackingUnit", "attrName")))

Categories