@mongoose643
dougnoel was actually trying to provide a good example of how the if function automatically evaluates to false and goes no further if the first condition in an if expression fails can be used to optimize a script. In his example not everyone is an admin, and not all admins can delete. In his script it is an optimized failsafe to check if someone is an admin and has delete permissions while not wasting script time.
Control Structures
Table of Contents
Control Structures
captainf
09-Mar-2007 04:53
09-Mar-2007 04:53
mongoose643 at gmail dot com
01-Mar-2007 03:03
01-Mar-2007 03:03
@simplyduh
>Duh, both are booleans in the above case, and its obvious
>that checking one boolean is better than checking 2.
>if ($has_delete_permissions) would work better :)
I read dougnoel's post. He was providing an example of a BAD example. The last line in his post suggests that you NOT check for both - only the admin. So actually what he meant to check for is whether or not they are an admin. If so then they automatically have delete permissions - therefore it may be possible that no variable named "$has_delete_permissions" exists. The resulting statement would be as follows:
if($is_admin)
{
//Statements to delete stuff go here.
}
simplyduh
04-Nov-2006 10:33
04-Nov-2006 10:33
dougnoel, you are wrong recarding optimization:
> if ($is_admin && $has_delete_permissions)
> If only an admin can have those permissions, there's no
> need to check for the permissions if the user is not an admin.
Duh, both are booleans in the above case, and its obvious that
checking one boolean is better than checking 2.
if ($has_delete_permissions) would work better :)
jupiter at nospam dot com
28-Aug-2006 10:46
28-Aug-2006 10:46
// assigning a variable inside an IF conditional does assign the value,
// then if it evaluates to true, continues to the true statement group
<?php
$a = array(1, 2, ' ', true, 0, '', false, array());
foreach ($a as $b) {
if ($c[] = $b) {
echo 'true, ';
} else {
echo 'false, ';
}
}
print_r($c);
/* RETURNS
true, true, true, true, false, false, false, false
Array
(
[0] => 1 // integer
[1] => 2 // integer
[2] => // a single space
[3] => 1 // boolean true
[4] => 0 // integer
[5] => // nothing
[6] => // nothing
[7] => array()
)
*/
?>
// Notice $c[4] and $c[7] are assigned values, but then evaluate to false
christopher dot metz at lavafrog dot net
06-Aug-2006 01:34
06-Aug-2006 01:34
in the two previous examples, both are correct.
for $foo = false, you're assigning the value while operating in the condition, so its just like:
$foo = false;
if( $foo )
print( 'a' );
else
print( 'b' );
....returns 'b';
for $foo == false, it will return true or false based on if $foo equals false or true, respectively.
28-May-2006 10:07
$foo=false will never work as it is tha assign operator.
$foo == false is required
abu_hurayrah at hidayahonline dot org
24-May-2006 10:42
24-May-2006 10:42
Regarding lamfeust's example:
Just a note about initializing variable
<?php
if($foo=false)
print("true");
else
print("false");
// this print on screen false
?>
This will print out "false" due to the expression "$foo=false" returning the value of the assignment - boolean false, in this case.
I'm not sure what, exactly, that had to do with initialization.
dougnoel
06-May-2006 03:29
06-May-2006 03:29
Further response to Niels:
It's not laziness, it's optimization. It saves CPUs cycles. However, it's good to know, as it allows you to optimize your code when writing. For example, when determining if someone has permissions to delete an object, you can do something like the following:
if ($is_admin && $has_delete_permissions)
If only an admin can have those permissions, there's no need to check for the permissions if the user is not an admin.
roan dot kattouw at home dot nl
09-Dec-2005 01:49
09-Dec-2005 01:49
In response to Niels: The ANSI C standard also dictates this laziness .
niels dot laukens at tijd dot com
26-Dec-2004 04:49
26-Dec-2004 04:49
For the people that know C: php is lazy when evaluating expressions. That is, as soon as it knows the outcome, it'll stop processing.
<?php
if ( FALSE && some_function() )
echo "something";
// some_function() will not be called, since php knows that it will never have to execute the if-block
?>
This comes in nice in situations like this:
<?php
if ( file_exists($filename) && filemtime($filename) > time() )
do_something();
// filemtime will never give an file-not-found-error, since php will stop parsing as soon as file_exists returns FALSE
?>