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.
Related
This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }
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.
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)
This question already has answers here:
Check if url contains parameters [duplicate]
(3 answers)
How to verify if $_GET exists?
(7 answers)
Closed 9 years ago.
I have lots of PHP statements along the lines of:
$getvalue = $_GET['valueiwant'];
In some scenarios not all variables are available. So, let's say 'valueiwant' doesn't exist in the URL string, how can I return a value based on the fact it doesn't exist?
For example if 'valueiwant' can't be found set $getvalue to -1
Currently it appears the value defaults to 0 and I need to be equal less than 0 if it doesn't exist.
Any ideas?
thanks
I always use
$getvalue=isset($_GET['valueiwant'])?$_GET['valueiwant']:-1;
Use of the isset() function checks if the offset exists, and returns a boolean value indicating it's existence.
This means that you can structure an if statement around the output.
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