How are multiple expressions in an "if" statement interpreted? [duplicate] - php

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 years ago.
Basically
Is this:
<?PHP if (false && crazyFunction()) : ?>
The same as:
<?PHP if (false) : ?>
<?PHP if (crazyFunction()) : ?>
If FALSE is evaluated in the first example will it still continue to evaluate "crazyFunction"?

The && operator is a shotcircuit operator, which means that it will stop as soon as it knows the outcome is going to be false.
This means that if the left part evaluates to false it stops and returns false. crazyFunction() will never be called in this example.

As soon as the value of boolean expression is known, it is no more executed.
Please note this (deprecated) example in old-fashioned mysql connections:
$db=mysql_connect_db('...') or die('Database error');
If after first part mysql_connect returned something that is not FALSE, 0, NULL etc., it does not execute this die().
(Regardless if this is or not correct to use mysql_* functions)

Related

How does the if statement work with data returned from PHP methods? [duplicate]

This question already has answers here:
How exactly does if($variable) work? [duplicate]
(8 answers)
Closed 3 years ago.
Looking at this single if-statement, if a string is returned, then the condition of the if statement is false? and if null is returned the condition is true? That doesn't seem to make sense, though I know that, that is how this piece of code works. Does null always mean true? and does a stand alone string always get inturpruted as a false condition for an if statement?
BTW, this is just somthing ive been racking my head on, doesnt really matter, other than clarity sure would feel good.
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);}
tl;dr: See the table here, particularly the "Boolean" (far right) column.
A string will only evaluate to false if it contains zero characters (an empty string) or the single character "0" (zero), in which way it behaves like the integer zero. Otherwise, it will always evaluate to true.
Null will always evaluate to false. If it seems to be evaluating as true, there must be something else happening there.
If you want to be sure, you can use the identity operator === and/or its negative compliment !== or your own casting to be sure.
if ($conn->connect_error !== null) {
// …
}

PHP logical operator [duplicate]

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 4 years ago.
When the isset() is executed the following check $_SESSION['mat'] == "1" is executed or the second verification is directly skipped since it is the first false?
Is there is where I have the doubt?
if(isset($_SESSION['mat']) and $_SESSION['mat']=="1"){}
If the first part of your if statement already returns false the second part will not be evaluated. Your if statement looks good this way and shouldn't throw any index out of bounds errors.
When the isset is executed the following check is executed ($ _ SESSION ['mat'] == "1") or the second verification is directly skipped since it is the first false.
isset() will check if a variable is set otherwise it will return false. All the code inside the if() statement will be executed, so in your case, if the $_SESSION array variable isn't set, the second control, in your case and (that can be expressed also using &&) will be skipped.

IMPORTANCE OF LOGICAL NOT IN PHP [duplicate]

This question already has answers here:
How does true/false work in PHP?
(6 answers)
Closed 9 years ago.
Is it same in PHP:
if($x!=5)
{
//code
}
VS
$x=5;
if(!$x)
{
//code
}
What about if($x)? Expression in IF statement evaluates to either TRUE or FALSE unlike C where it is either 0 or anything other than 0 (say 1 or more). We can test the expression by using var_dump(!$x) in PHP. So,what about if($x)?
They are not the same.
The first block of code tests whether or not the variable x does not equal 5.
The 2nd block of code tests whether x is not true. Since you declared a value for $x, the statement will be evaluated as false and the content inside the brackets will not execute.
No,it is not same in PHP:
Logical Operator.
! $x Not TRUE if $x is not
TRUE.
Comparison Operators
$x!=5 Not equal TRUE if $x is not equal to 5
Source: PHP Documentation.

PHP executing header:location regardless IF statement parameters [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The 3 different equals
Can anyone tell me why, when using the code below, I am getting redirected to elephant.com rather than seeing a 'giraffe!
<?php
$foo="giraffe";
if($foo="elephant"){
header("location:http://www.elephant.com");
exit();
}else{
echo $foo;}
?>
Thanks for looking
J
if($foo="elephant")
You're assigning $foo here, rather than comparing it; you should be doing:
if($foo=="elephant")
The result of an assignment operation is the value that's just been assigned; in this case, 'elephant' is evaluating to true.
Your if() statement has a single equal sign. This doesn't do a comparison in PHP; it sets the value and returns true.
In order to do a comparison, you need to use either a double-equal or a triple-equal sign:
if($foo == "elephant") { .... }
or
if($foo === "elephant") { .... }
The difference between the two is that double-equal doesn't care about the variable's data type, whereas triple-equal does. In this case, there's not much difference between them, but it's worth learning and understanding the differences because they can bite you if you don't know them. More info here: http://php.net/manual/en/language.operators.comparison.php

php syntax: program flow control using the || operator [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP short circuit lazy evaluation, where is it in the php.net manual?
PHP “or” Syntax
I have seen people using the || operator as program flow control as follows:
function() || die("message");
where die("message"); will run if function() returns false. Furthermore, it seems to only work for die(); and exit(); else the interpreter will throw a "syntax error" message.
I'm wondering what this is called and where can I find documentation for its behaviour.
It's just a boolean OR expression. The usage is taking advantage of a behavior called short cutting, where if the first part of the expression evaluates to true, then the second half isn't evaluated because the OR expression is already true.
It's just logic OR. If function() returns true, then the rest of the expression is not evaluated.
This is due to OR / || being an operator with left precedence (see here: http://www.php.net/manual/en/language.operators.precedence.php) as the left is evaluated to be true, there is no point in evaluating the right side as the expression will always be true.

Categories