This question already has an answer here:
What does ( !'which npm' ) mean in a PHP script?
(1 answer)
Closed 4 years ago.
I have used if(!$url) but I don't know the exact meaning of the below code in PHP
Can we check string with logical operator
if (! 'redirect cart'){
// data
}
Is it allow to use in a logical operation?
It equals to comparing to false:
if (false == 'redirect cart') { ...
Which could be true if your string is empty:
if (! '') { // this condition will be met
But this makes no sense to write this condition with a direct string, this should be a variable:
$str = my_function() ? 'redirect cart' : '';
if (!$str) { ...
The ! Operator negates the value of the expression. Code like this $value = (!true); will return false i.e $value will be false. The same happens when you apply the operator to an expression that returns false
Related
This question already has answers here:
logical vs assignment operator precedence in PHP
(1 answer)
'AND' vs '&&' as operator
(10 answers)
PHP Logical Operators precedence affects variable assignment results strangely
(3 answers)
Closed 3 years ago.
Take a look at this code:
if ($the_string = a_function_call('someid123') && $another_string = another_function_call('someid123'))
{
// $the_string and $another_string are now both expected to be the values returned from their respective functions, and this block of code is expected to be run if both of them are not "falsey".
}
However...
if ($the_string = a_function_call('someid123') && $another_string = another_function_call('someid123'))
{
// $the_string is now actually a boolean true. Not sure about $another_string.
}
Is this a weird bug? Or the intended behaviour? I've always thought that it was like the first scenario, and I've always coded by that assumption.
&& has higher precedence than =. So your code is being parsed as if you'd written
if ($the_string = (a_function_call('someid123') && ($another_string = another_function_call('someid123'))))
This performs the following steps:
Calls a_function_call()
If that returns a truthy value, it calls another_function_call() and assigns the result to $another_string().
Assigns the truth value of the && expression to $the_string
Tests that truth value in the if statement.
This precedence allows you to write code like:
$success = function_1() && function_2();
If = had higher precedence, that would set $success just from function_1(), not the combination.
Adding parentheses would solve the problem:
if (($the_string = a_function_call('someid123'))
&& ($another_string = another_function_call('someid123')))
PHP also has and and or operators that are like && and ||, but they have lower precedence than assignment; they exist to address this issue. You often see them used in code like this:
$variable = some_function(...) or die("some_function didn't work");
So simply replacing && with and would also solve the problem.
The key is the operator precedence.
The expression is evaluated as follow (pay attention to the parentheses):
if ($the_string = (a_function_call('someid123') && $another_string = another_function_call('someid123')))
$another_string takes the value from another_function_call() than is checked against a_function_call() return value through an AND operator.
Add the correct parentheses as follow to get the expected result:
if (($the_string = a_function_call('someid123')) && ($another_string = another_function_call('someid123')))
Please check the php operators precedence.
This question already has answers here:
PHP : Difference between '&&' and 'AND' [duplicate]
(1 answer)
'AND' vs '&&' as operator
(10 answers)
Closed 7 years ago.
[update]
This isn't a duplicate question of "what's the difference between AND and &&" rather it's a question regarding the use of AND and the ternary operator. anyway i think i got my answer i didn't know that all operators have precedence which alex kindly pointed out. http://php.net/manual/en/language.operators.precedence.php
[original]
echo TRUE && TRUE ? 'A' : 'B';
// Outputs: A
echo TRUE AND TRUE ? 'A' : 'B';
// Outputs: 1
I know that the ternary operator syntax is
condition ? true : false
and the difference between && and AND is evaluation precedence (of the condition)
e.g. TRUE AND TRUE && TRUE is evaluated at (TRUE AND (TRUE && TRUE))
so shouldn't using both AND and && yield the same return? is this a PHP bug? (im using ver5.5.23)
or should i just accept the fact that if i used AND in the condition it actually becomes excluded from the condition
e.g.
TRUE AND TRUE ? 'A' : 'B' => (TRUE AND (TRUE ? 'A' : 'B'))
This question already has an answer here:
symbol and decimal number true in php
(1 answer)
Closed 8 years ago.
I have script like this
if('#' == 0){
echo "true";
}else{
echo "false";
}
output :
true
the question is why it get true and how to get it false?
thanks.
As Mark Baker stated in the comments, PHP uses weak typing. According to the comparison matrix that you find here (http://www.php.net/manual/en/types.comparisons.php), the expression involving the string '#' compared to 0 will be evaluated as true.
Change your condition to:
if('#' === 0){
...
in this case you'll get false.
For this condition to get true of false properly, you need to compare the value and the type of the operators. Try this:
if('#' === 0){
echo "true";
}else{
echo "false";
}
You will get false for this case.
This question already has answers here:
Variable position in comparision in PHP
(4 answers)
Closed 9 years ago.
what is the difference between these two expressions ,
if(false == $flag)
if($flag == false)
what is the benefit of this expression rather than normal initiation like this :
if($flag == false)
In first case, if(false == $flag), compiler warn you if you type accidentally = instead of ==. But this is not true in the second case, if($flag == false).
In second case if == is accidentally replaced by = then compiler do not show you any warning or error instead $flag = false will assign false to flag and the condition $flag = false will always be false.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Getting confused with empty, isset, !empty, !isset
In PHP what is the difference between:
if(!isset)
if(isset)
Same with if(!empty) and if(empty)?
What does the "!" character mean?
! is the logical negation or NOT operator. It reverses the sense of the logical test.
That is:
if(isset) makes something happen if isset is logical True.
if(!isset) makes something happen if isset is logical False.
More about operators (logical and other types) in the PHP documentation. Look up ! there to cement your understanding of what it does. While you're there, also look up the other logical operators:
&& logical AND
|| logical OR
xor logical EXCLUSIVE-OR
Which are also commonly used in logic statements.
The ! character is the logical "not" operator. It inverts the boolean meaning of the expression.
If you have an expression that evaluates to TRUE, prefixing it with ! causes it evaluate to FALSE and vice-versa.
$test = 'value';
var_dump(isset($test)); // TRUE
var_dump(!isset($test)); // FALSE
isset() returns TRUE if the given variable is defined in the current scope with a non-null value.
empty() returns TRUE if the given variable is not defined in the current scope, or if it is defined with a value that is considered "empty". These values are:
NULL // NULL value
0 // Integer/float zero
'' // Empty string
'0' // String '0'
FALSE // Boolean FALSE
array() // empty array
Depending PHP version, an object with no properties may also be considered empty.
The upshot of this is that isset() and empty() almost compliment each other (they return the opposite results) but not quite, as empty() performs an additional check on the value of the variable, isset() simply checks whether it is defined.
Consider the following example:
var_dump(isset($test)); // FALSE
var_dump(empty($test)); // TRUE
$test = '';
var_dump(isset($test)); // TRUE
var_dump(empty($test)); // TRUE
$test = 'value';
var_dump(isset($test)); // TRUE
var_dump(empty($test)); // FALSE
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
Edit:
here is a test case for you:
$p = false;
echo isset($p) ? '$p is setted : ' : '$p is not setted : ';
echo empty($p) ? '$p is empty' : '$p is not empty';
echo "<BR>";
$p is setted : $p is empty