PHP AND OR , Is this valid? - php

Is this valid? I want to check if 'One' is not blank AND two is either blank or undefined. Can i use the brackets that way around both of GET[two]?
if( $_GET["one"] == '' && ($_GET["two"] == '' || $_GET["two"] == 'undefined') ) {
// do X
}
Thanks

Yes, it's certainly valid. Basically, your if checks if BOTH cases (1) one == '' and (2) two == '' or two == 'undefined' exists. (2) evaluates as true if two is either '' or 'undefined'
The brackets are perfectly valid. :)

Yes, that is valid. It should do exactly what you want.
If you need to check for empty strings or NULL values, you can also use the empty() function:
if (empty($_GET['one']))

Related

PHP Is one value set and not another

I'm working on a truthy/falsy where I need to check 2 values, of which one may be null, the other 0. If integer vs string value same, they'd be the same.
The simplest I've been able to break it down is:
if($var1 == $var2) {// They may be the same
if((isset($var1) && !isset($var2)) || (!isset($var1) && isset($var2))) { // Is one set, and not the other?
return true; // As different as we'll allow
}
return false; // Nope they're the same
}
return true; // Definitely different
My question would be mainly, given the above code block, is there a simpler way to check if one value is set, and not the other?
EDIT: The reason I check for equality first, is that the value may pass 1 == 10 or 10 == '10', in which case, I don't need to check further, but if null == 0, this would pass and require further checking. I saw that I can pass multiple values to isset, which would work if I required both to be set or both not set, but if one is, and not the other, that's where I need to flag.
Using the null coalescing operator may come in handy...
return ($var1 ?? null) !== ($var2 ?? null);
Checks that they are not equal and at least one is defined and not null.
You are checking for equality before checking if they are set.
I'd recommend checking if they are set first.
If the PURE goal is to check if one is set and not the other, then:
return ( ( empty($var1) && ! empty($var2) ) || ( ! empty($var1) && empty($var2) );
Per comments, isset is the requirement, so this version:
return ( ( isset($var1) && ! isset($var2) ) || ( ! isset($var1) && isset($var2) );
And, props to #deceze to suggest an even simpler version:
return ( isset($var1) != isset($var2) );

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...')))

Complicated `if`case, correct?

I'm trying really hard to figure out how to solve this problem. Please be gentle, i'm still learning!
There are four workplaces named: B.Avf, R.Avf, Office and Production. Products is passing theese workplaces. Now i'm building a script that can alert if the same product is passing the same workplace twice. Now, this is the hard part: If the product has passed "B.Avf" it can't pass "R.Avf" whitout an alert, same if the product has passed "R.Avf" it can't pass "B.Avf" whitout and alert. But if the product has passed for ex. "R.Avf" it´s ok to pass "Office" whitout an alert.
This is what i got so far: (It's like example number 5 =)
Maby should i create nested ifstatements instead?
This code will turn true all times!
PHP
if($_SESSION['user']['usr_workplace'] == "R.Avf" ||
$_SESSION['user']['usr_workplace'] == "B.Avf" &&
strpos($history, "R.Avf") !== FALSE ||
strpos($history, "B.Avf") !== FALSE)
Your if condition should be :
//IF this session user workplace is "B.Avf" or "R.Avf"
//AND "B.Avf" or "R.Avf" excist in history, then alert!
if(($_SESSION['user']['usr_workplace'] == "R.Avf" || $_SESSION['user']['usr_workplace'] == "B.Avf") && (strpos($history, "R.Avf") !== FALSE || strpos($history, "B.Avf") !== FALSE))
Your if statement can be made a lot simpler by removing one condition and still getting the desired result like so:
if(
($_SESSION['user']['usr_workplace'] == "R.Avf" || $_SESSION['user']['usr_workplace'] == "B.Avf")
&&
strpos($history, $_SESSION['user']['usr_workplace']) !== false
)
Notice how you don't need two strpos checks, as the first part of the if statement will only allow the second part to run if it was true.
It would be better to extract this to a method or simplify it further, but for what you've asked this will do :).
This works, it has the right braces:
if($_SESSION['user']['usr_workplace'] == "R.Avf" || ($_SESSION['user']['usr_workplace'] == "B.Avf" && (strpos($history, "R.Avf") !== FALSE || strpos($history, "B.Avf") !== FALSE)))
It checks for r.Avf first, OR and then checks all the conditions within the braces
you should break this if into 2 methods.
one should be called:
isUserSessionRBAvf($_SESSION['user']['usr_workplace'])
the other should check the strpos pos:
isHistoryAvf($history, "R.Avf")
and you would end up with just:
if(isUserSessionRBAvf(...) && isHistoryAvf(...))
this way you have a more readable code and easier to debug.
P.S. consider a different method naming
what you need to do is simply check with session value to $history so you will avoid checking two strpos() where you are doing static string checking:
here you are checking with static string:
strpos($history, "R.Avf") !== FALSE ||
strpos($history, "B.Avf") !== FALSE)
make it dynamic like this:
if($_SESSION['user']['usr_workplace'] == "R.Avf" || $_SESSION['user']['usr_workplace'] == "B.Avf" && strpos($history, $_SESSION['user']['usr_workplace']) !== FALSE )

PHP Short If Statment with OR

I have the following PHP script
<?php
echo (($statusSet) == 'all') ? "class='selected'": "";
?>
What I would like to do is include an OR into this to say where $statusSet is equal to all OR NULL
Im competely lost with hwo to write this as I tried adding the normal type of OR statement which didnt work
<?php
echo ((($statusSet) == 'all')||(($statusSet) == 'all'))) ? "class='selected'": "";
?>
All you're doing is adding another expression to be evaluated in the overall if statement.
<?php echo (($statusSet == 'all' || $statusSet === null) ? "class='selected'": ""); ?>
Someone below posted a nearly-identical snippet to mine, but used is_null() instead. Note that using is_null() or === null is fine, but using == null isn't best practices - it won't ensure type equality so if $statusSet was set to (int)0, doing $statusSet == null would return true when it's actually not a null value.
So like this?
echo $statusSet == 'all' || $statusSet === NULL ? "class='selected'": "";?>
I'm not sure that you copied the correct line into the question, but the reason that yours isn't working is because the two conditions are exactly the same, hence if one is true then then so is the other.
<?php echo (($statusSet == 'all') || (is_null($statusSet)) ) ? "class='selected'": "";?>

php,symfony 2 filter case sensitive

I want to ignore case in a filter , My code:
if (strtolower(isset($filterObject['name'])) && null !== strtolower(($filterObject['name']))) {
$queryFilter->addStringFilter("name", ($filterObject['name']));
}
If you want to ignore the casesensitivity of the addStringFilter by lowercasing the object
you would just have to use strtolower($filterObject['name']).
strtolower lowercases the string given.
On that note you are using strtolower on an isset function result which won't do a thing there (as isset returns no string).
So you should change your sourcecode to:
if (isset($filterObject['name']) && null !== strtolower(($filterObject['name'])))
{
$queryFilter->addStringFilter("name", strtolower(($filterObject['name'])));
}
Btw one case you didn't check is if $filterObject['name'] is empty (not sure if it is possible as I don't know your remaining code. If it CAN be possible you want to add another and into the if:
&& $filterObject['name']
That would make sure that it is filled with more than an empty string.
Thus the if part would change to:
if (isset($filterObject['name']) && $filterObject['name'] && null !== strtolower(($filterObject['name'])))

Categories