Ternary operator with multiple conditions - php

Hello I am trying to properly format ternary operator to be using multiple conditions in php:
$result = ($var !== 1 || $var !== 2) ? '' : 'default';
The problem is that in this format I always get not true even iv the $var is 1 or 2. With one condition for example $var == 0 it is working fine. Any help will be welcome.

This statement will always be true:
($var !== 1 || $var !== 2)
Because $var can never simultaneously be both values, it will always not be at least one of the two values. Which satisfies the || operator.
If you want to know whether $var is one of the two values:
($var === 1 || $var === 2)
If you want to know if $var is neither of the two values, you can negate the condition:
(!($var === 1 || $var === 2))
Or individually negate the operators in the condition and use && instead of || (since all conditions need to be met to prove the negative, instead of just one condition to prove the positive):
($var !== 1 && $var !== 2)
Depending on readability and personal preference.

If $var is 0 , $result is "default" but 1 and 2 enter in condition, both are different from both and always enter.
$result = ($var !== 1 && $var !== 2) ? '' : 'default';

Related

PHP: Evaluate string as logical expression and return true or false

I have a txt file with hundreds of logical expressions.
I want to read each one (no problem so far) and to be able to evaluate it recursively, but I can't figure a way how. The expression has && and == and comparissons between strings and numbers. I don't want to use eval, as it's not recommended apparently and it didn't work in my case.
Example. Let's say I read these 2 strings:
s = "a == alpha && b == beta || b == omega", or
s = "g >= 2 && f != gamma"
I want to break them down to
($a == "alpha" && $b == "beta" || b == "omega")
($g >= 2 && f!= "gamma")
to use them in an if, so that it returns TRUE or FALSE. My problem is not with replacing the variables, it's with making them evaluate as a logical expression
Can anybody give me a hand?
Thanks in advance,
Cristina
Try this :
if( (($a == 'alpha' && $b == 'beta') || ($b == 'omega')) || ($g >= 2 && $f != 'gamma'))
{
// returns true
}
else
{
// returns false
}

How can i detect if (float)0 == 0 or null in PHP

If variable value is 0 (float) it will pass all these tests:
$test = round(0, 2); //$test=(float)0
if($test == null)
echo "var is null";
if($test == 0)
echo "var is 0";
if($test == false)
echo "var is false";
if($test==false && $test == 0 && $test==null)
echo "var is mixture";
I assumed that it will pass only if($test == 0)
Only solution I found is detect if $test is number using function is_number(), but can I detect if float variable equal zero?
Using === checks also for the datatype:
$test = round(0, 2); // float(0.00)
if($test === null) // false
if($test === 0) // false
if($test === 0.0) // true
if($test === false) // false
Use 3 equal signs rather than two to test the type as well:
if($test === 0)
If you use=== instead of == it will compare as well as get the data types errors manage...Can you post your answer while using === ? Please check the difference between this two here
When comparing values in PHP for equality you can use either the == operator or the === operator. What’s the difference between the 2? Well, it’s quite simple. The == operator just checks to see if the left and right values are equal. But, the === operator (note the extra “=”) actually checks to see if the left and right values are equal, and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).

Is this valid PHP syntax?

if ($var == ($var1 || $var2))
{
...
}
I am considering using this, but am ont sure if it is valid, and there doesn't seem to be somewhere to check.
It seems logically consistent to me but am not sure, and I don't have something to test it on close by.
If it is valid, what other mainstream languages support this sort of construct.
EDIT: The comparison is valid, but not in the way I was thinking.
What I was trying to do was actually the in_array() function, which I just discovered.
Your code is syntactical valid but semantical probably not what you wanted.
Because $var1 || $var2 is a boolean expression and always yields true or false. And then $var is compared to the result of that boolean expression. So $var is always compared to either true or false and not to $var1 or $var2 (that’s what you’re have probably expected). So it’s not a shorthand to ($var == $var1) || ($var == $var2).
Now as you already noted yourself, in_array is a solution to this problem if you don’t want to write expressions like ($var == $var1) || ($var == $var2), especially when you have an arbitrary number of values you want to compare to:
in_array($var, array($var1, $var2))
Which is equivalent to:
($var == $var1) || ($var == $var2)
If you need a strict comparison (using === rather than ==), set the third parameter to true:
in_array($var, array($var1, $var2), true)
Which is now equivalent to:
($var === $var1) || ($var === $var2)
Yes, the corrected version is valid syntax:
if ($var == ($var1 || $var2))
Question is, what does it mean?
It will compare the result of the expression ($var1 || $var2) which will be a boolean, to the value of $var.
And, as mentioned, php -l file.php will tell you if there are any syntax errors.
Edit:
Consider this:
$var1 = 1;
$var2 = 2;
echo var_dump(($var1 || $var2));
Result is:
bool(true)
You can use the command php -l filename.php from the command line to check for syntax errors.
As George Marian says, it's missing a closing parenthesis so would throw a syntax error. It's otherwise valid, though, so I can't see that it's the logical OR construct itself that you're unsure about. It's used in several languages, including javascript.
your corrected example is valid and will be TRUE is $var is TRUE and either $var1 or $var2 is TRUE .. OR . if $var, $var1 and $var2 are all FALSE

Handling of 'or' conditions in Ruby vs PHP

I'm not sure what to call this, so I'll give an example.
In PHP
1==2 || 2 returns 1 or true
In Ruby
1==2 || 2 returns 2 (The second statement if the first evaluates to false).
Is there any short way to implement similar thing in PHP?
How about
1==2 ? 1==2 : 2
or in PHP 5.3
1==2 ?: 2
In PHP, the result of boolean expressions is always a boolean. So 1==2 || 2 gives true.
The best thing I can think of is
($var = 1 == 2) || ($var = 2)
Then $var will be 2.
Depending on the answer to Matchu's question, you may want:
(($var = 1) == 1) || ($var = 2)
or
($var = 1 == 1) || ($var = 2)
How about 1 == 2 or 2?
However, the result might not be the same if you print directly, so you need to put the result inside a variable. Take this example:
$result = "a" or 2;
var_dump($result); // prints string(1) "a"
var_dump("a" or 2); // prints bool(true)
Take a look here: http://www.php.net/manual/en/language.operators.logical.php

PHP if or statement not working

We are trying to use the below piece of code
if (($_GET['1'] != "1") || ($_GET['1'] != "2")) {
When we try this no matter what value the variable has it will evaluate as true even when data is entered that is false. When we use
if (($_GET['1'] == "1") || ($_GET['1'] == "2")) {
and put in data that will make it return false it works correctly. We have reversed the way that the if statement goes just so we can get this working but i would like to know why this doesnt work, if it is something im doing wrong or a limitation within php with the or and the not equal operators
Thanks
Your first test is saying this:
If $_GET['1'] is anything other than "1" OR $_GET['1'] is anything other than "2"
The expression will always pass: If it equals 1 it will pass the != '2' test on the second half of your if statement. If it equals 2 it will pass the != '1' test on the first half, and never make it to the second half of the test.
The second test simply says:
If $_GET['1'] equals "1" OR $_GET['1'] equals "2" then the expression should pass
You probably want this expression, which will pass only if neither parameter holds the correct value:
if(($_GET['1'] != '1') && ($_GET['1'] != '2'))
The expression below will always evaluate to TRUE, because either x is not 1 or x is not 2. There is no way that x can equal both 1 and 2 at the same time!
($x != 1) || ($x != 2)
The opposite of
($x == 1) || ($x == 2)
is
($x != 1) && ($x != 2)
Note that you have to change the || to a &&.
Try:
if (($_GET['1'] != "1") && ($_GET['2'] != "2")) {
If you use != in both statements, then you need to use && instead of || to make sure both statements get evaluated.
Essentially, using || means that the first statement gets evaluated and if it is false, then PHP will ignore the second statement because you are using OR. But if you use &&, then PHP will be sure to evaluate both != statements.
if your code is:
if (($_GET['1'] != "1") || ($_GET['2'] != "2")) {
any value or even null value can enter this condition. you can change || to && meaning any string which is not equal to "1" and "2" can enter you condition
Without knowing the intention of the statement, or what input you started with (or how you revised it), I can't say for certain what the issue is.
That said, the first statement will only evaluate to FALSE if $_GET['1'] is equal to 1 and $_GET['2'] is equal to 2.
The second statement will only evaluate to FALSE if $_GET['1'] is not equal to 1 and $_GET['2'] is not equal to 2.
I'm guessing what you wanted on your first statement was
if (($_GET['1'] != "1") && ($_GET['2'] != "2")) {
which evaluates to FALSE unless $_GET['1'] is equal to 1 and $_GET['2'] is equal to 2.

Categories