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

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"

Related

PHP - if single value equals multiple options

So I understand this - and comparing/checking values. However, I was messing about and noticed the outcome for all my tests were the same - some of which I was taught (a) didn't work or (b) was incorrect.
Note, I'm running PHP7. Okay, to my point. I was able to achieve the same outcome checking if a single value equals one of multiple options...
These work...why? Def not the way I learned.
if ($status == 'in-progress' || 'in-review')
// and even
if ($status == ('in-progress' || 'in-review')) // kind of similar to ASP.NET Razor
I normally would repeat the check, like so: if($stat == 'a' || $stat == 'b') or even in_array() which is essentially the same thing.
Is the first examples, correct? If not, why is it working? Or is this something frowned upon and not practiced - or maybe even something new?
First off to make it clear == has a higher precedence than ||. This means your two if statements look like this:
if (($status == 'in-progress') || 'in-review')
if ($status == ('in-progress' || 'in-review'))
Now for your first if statement regardless what value $status has and what the outcome of ($status == 'in-progress') is, since you have an OR in it and after it 'in-review' your if statement will always be true, since a non empty string is a truthy value.
For your second statement, this part ('in-progress' || 'in-review') comes literally down to TRUE || TRUE, which evaluates to TRUE. Now $status just needs to hold a truthy value and the if statement will be true.
No, that code will never work. || has a lower precedence than ==, so you're comparing $status against the first value, then boolean || "or" the other value
if (($status == 'foo') || ('bar'))
You have to compare the values individually:
if (($status == 'foo') || ($status == 'bar'))
And this gets tedious for many values. A quick hack is to use an array:
if (in_array($status, array('foo', 'bar', 'baz', 'qux', 'etc...')))

How to use an IF statement to ensure one is always true, and either of the others is true

I am trying to do this in a single IF statement to avoid having duplicated code, I would like to know if it is possible to do what I want to do.
I originally had this code to run code on all categories that doesn't equal '15', Now I need it to not apply to categories 15 and 57.
Original code:
if ($level==1 && $category_id!=15){
New code (the general idea):
if ($level==1 && ($category_id!=15 || $category_id!=57)){
Edit: If downvoting, please explain why. I have tested the code above and it always returns false.
The following code means that if will be true when $level equals to 1 and $category_id doesn't equal to none of 15, 57.
if ($level == 1 && !in_array($category_id, array(15, 57)))
Have a look here: in_array
It should be:
if ($level==1 && $category_id!=15 && $category_id!=57){
You can just use multiple and criteria:
if ($level==1 && $category_id!=15 && $category_id!=57){
The reason your current code fails (it will always return true when $level==1) is because $category_id can never be both values.
let $category_id ==15 then the left side of the condition ($category_id!=15 || $category_id!=57) will evaluate to false (15 != 15) == false so the right side will be evaluated (15 != 57) == true causing the overall statement to evaluate to true.
let $category_id ==57 then the left side of the condition ($category_id!=15 || $category_id!=57) will evaluate to true (57 != 15) == true so the right side is never evaluated, causing the overall statement to evaluate to true.

PHP Syntax: "or" in "If" Statement [duplicate]

This question already has answers here:
How to have multiple conditions on the same if statement? [duplicate]
(5 answers)
Closed 8 years ago.
hello I just wanted to check and see if this would be correct PHP syntax:
if ($input == "DeOnTRAY96#localhost"){
echo"
Projects: 1"?>
<br>
<?php
echo"
Admin: yes
";
}
elseif ($input == NULL){
die("Please enter password.");
}else{
header("Location:Invalidpassword.php");
exit;
}
Right where is says
if($input == "DeOnTRAY96#localhost"){
Could I put
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
And still have it work?
You don't want
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
You want
if($input == "DeOnTRAY96#localhost" or $input == "somethingelse"){
I might suggest using === instead of == in this case, as you'd like a type sensitive comparison.
Additionally, for $input == NULL you should use is_null($input). Null is weird in most programming languages, so language specific functions for testing are usually the way to go (rather than comparison)
OR syntax in PHP:
if($var == 'something' || $var == 'something else')
{
//do something
}
For reference:
|| means OR
&& means AND
For a more future-proof solution, consider in_array. I use it for as few as two options, if there's even the slightest chance there may be more added.
if( in_array($input, ["DeOnTRAY96#localhost", "somethingelse"]))
Once you get to four or more options, it's probably better to do something more like:
$whitelist = [
"DeOnTRAY96#localhost"
, "somethingelse"
, "anotheroption"
, "too many options to do inline!"
];
if( in_array($input, $whitelist))

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

Using OR in a IF

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
}

Categories