Using OR in a IF - php

Any ideas why this isn't working?
if($page->Slug != 'water-filters' || $page->Slug != 'pet-care' || $page->Slug != 'books') {
//do something
}

I think you mean AND instead of OR because you're using not equals.
By using not equals in the way you are the statement will always be true, if $page->Slug equals 'water-filters' it doesn't equal 'pet-care' and hence the if statement as a whole returns true.
if($page->Slug != 'water-filters' && $page->Slug != 'pet-care' && $page->Slug != 'books')
{
//do something
}

I'm guessing that "do something" is always getting executed?
if($page->Slug != 'water-filters' || $page->Slug != 'pet-care' || $page->Slug != 'books')
{
//do something
}
For any value of $page->Slug, it will always be not equal to ONE of those three conditions, therefore at least one (technically, at least two) of the statements will always be true. Since you're using an 'OR' as long as one of the three statements is true, the whole thing will be true.
Therefore, this is essentially saying
if (true) {
//do something
}

$page->Slug is either 'water-filters' or 'pet-care' or 'books'

Try
== or == or ==
or
!= and != and !=
:-D

If the Slug is not "water-filters" or is not "pet-care" or is not "books"...
Well, if it's one of those, or any other value, it's by definition not the other two (or not all three). So this condition is always true.

Aside from what the others have said above, which are correct also, try this syntax for readability.
if(!in_array($page->Slug, array('water-filters', 'pet-care', 'books')) {
// Do something
}

Related

"If" statement comparison operators sequence of operation [duplicate]

This question already has an answer here:
How to get OR(||) and AND (&&) statements to work together? PHP
(1 answer)
Closed 3 years ago.
I have an "if" statement I would like to write with multiple comparison operators of the "&&" and "||" type. I am not entirely sure if it will perform the way I am thinking it should. Here is an example of what I am trying to do.
if($alpha == "FF" && $bravo == "3sh" || $charlie == "6sh")
{
printf($alpha);
}
What I expect is that $alpha MUST equal "FF" in order for this to execute.
What I also expect is at least one of the other two conditions must be met in order to execute.
What I am concerned about is the code ignoring the first two conditions and executing the code because the last condition is met.
You can create wrap conditions accordingly as below:
if($alpha == "FF" && ($bravo == "3sh" || $charlie == "6sh"))
{
printf($alpha);
}
So here, $alpha == "FF" should have to be true. And either one condition should be true in between on braces. Hope it helps you.
It may be helpful to look into Boolean logic a litte more, but for your particular situation what you are trying to do is:
if($alpha == "FF" && ($bravo == "3sh" || $charlie == "6sh"))
{
printf($alpha);
}
Essentially, you say
$alpha == "FF" must be true AND
($bravo == "3sh" || $charlie == "6sh") must be true
- i.e., either $bravo == "3sh" OR $charlie == "6sh"

Correct way with in_array

I have a problem with in_array giving me a notice: undefined index.
if (
($value['device'] == $this->device or $value['device'] == 'all')
and
(!is_array($value['excludeLang']) or !in_array($this->lang, $value['excludeLang']))
and
($value['site'] == $this->site or $value['site'] == 'all')
) {
// do something
}
In this case $value['excludeLang'] is not an array. Is there a way of suppressing the error when I check if $this->lang is in the array
(!isset($value['excludeLang']) or !is_array($value['excludeLang']) or !in_array($this->lang, $value['excludeLang']))
I would use isset(), specifically with your example I'd write it as follows:
if (
($value['device'] == $this->device or $value['device'] == 'all')
and
(!isset($value['excludeLang']) or !is_array($value['excludeLang']) or !in_array($this->lang, $value['excludeLang']))
and
($value['site'] == $this->site or $value['site'] == 'all')
) {
// do something
}
The reason this works, and I think it's important to bear this in mind, is because of the way || (or) conditions are evaluated in PHP.
Take the condition
if ($condition1 || $condition2)
Only one thing in an or statement needs to be evaluated as being true. PHP is evaluated left to right, and so in this case if $condition1 is evaluated as true, PHP won't bother to check $condition2.
In your specific case, if !isset($value['excludeLang']) is evaluated as being true, it will never get as far as trying to run in_array, it will skip all the other conditions, return true and continue.

IF OR condition isn't working as expected

I have a for loop, but I want to skip some of the loops if a condition is met.
This works fine, and will continue the loop if $option_nm isn't "Fonts".
if($option_nm != "Fonts"){
continue;
}
However, this doesn't work, and skips everything.
if($option_nm != "Fonts" || $option_nm != "Style" || $option_nm != "Thread" || $option_nm != "Textbox"){
continue;
}
I also tried changing || to Or which does the same thing.
What am I doing wrong?
You are confusing the OR statement.
Your code should be like:
if($option_nm != "Fonts" && $option_nm != "Style" && $option_nm != "Thread" && $option_nm != "Textbox"){
continue;
}
Your if condition always return true and loop goes continue, because your if condition you use or or ||.
try this one.
if($option_nm != "Fonts" && $option_nm != "Style" && $option_nm != "Thread" && $option_nm != "Textbox"){
continue;
}
Even though your post is over a year old, I'll give another solution.
You can utilize the in_array functionality in php and do an negative if statement around your statement.
$elements = array("Fonts", "Style", "Thread", "Textbox")
if (!in_array($option_nm, $elements)) { continue;}
This makes future maintenance simple and is easy to read.

Display HTML if two conditions are true or another two are true or third part of conditions are true

I have php if statement that should display certain HTML code if two conditions are true or another two are true or third part of conditions are true.
I have several arrays - $Options_arr, $MoreOptions_arr, $Special_arr .
To explain in the easiest possible way I want to do this:
if(!empty($Options_arr[0]) && $Options_arr[0]!="") or
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") or
(!empty($Special_arr[0]) && $Special_arr[0]!="")
{?> some HTML here
All help will be appreciated thank you.
empty() already checks for empty string "" so it's shorter:
if(!empty($Options_arr[0]) || !empty($MoreOptions_arr[0]) || !empty($Special_arr[0])) {
//some HTML here
}
BragG, you can use elseif
Like:
if((!empty($Options_arr[0]) && $Options_arr[0]!="") ||
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") ||
(!empty($Special_arr[0]) && $Special_arr[0]!=""))
{
// some html or any code
}
I hope that is what you were looking for..
Feel free to ask any question.
You are just missing some brackets. Also || is more frequently used than OR
if((!empty($Options_arr[0]) && $Options_arr[0]!="") || (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") || (!empty($Special_arr[0]) && $Special_arr[0]!="")){
echo '<p>hello</p>';
}
You're basically already there...
if (
(!empty($Options_arr[0]) && $Options_arr[0]!="")
|| (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="")
|| (!empty($Special_arr[0]) && $Special_arr[0]!="")
){
...do something
Basically you write an if statement that resolves if any of the sub-statements are true by joining the sub-statements together with ORs

Basic Boolean Logic -- How to test for condition only if another condition is true

I know this is basic boolean logic, but I'm stuck:
I am looping through database results, and for each one I need to check for the following condition:
if($old_value != $new_value)
If the above is true, the action is:
$old_value = $new_value;
But there is a secondary condition. If the row is of type "date", I need to also check that $new_value is not empty, but the action is still the same. Right now, I am doing it like this:
if($old_value != $new_value) {
if($type != date) {
$old_value = $new_value;
} elseif(!empty($new_value)) {
$old_value = $new_value;
}
I've oversimplified the above, but really that one-line action is actually several lines that I know I don't need to repeat based on the secondary condition.
But I'm at a loss on what the right way to combine the inner condition with the outer condition. If I do something like:
if(($old_value != $new_value) && ($type == 'date' && !empty($new_value))
Then the only time it would return true is when the row is of type date.
Try:
if(($old_value != $new_value) && ($type != 'date' || !empty($new_value))
That should do it. Let me know if you need help understanding why.
if(($old_value != $new_value) && ($type != 'date' || ($type == 'date' && !empty($new_value)))
Maybe this one can be made shorter but I can't think of how.

Categories