CONTROL STRUCTURE -- BOOLEAN REQUIRED
If you are not sure what will work in your IF statements, try DISECTING your variables. Below I've written three (3) empty CLASS DEFINITIONS (Point, Dimension, and Rectangle), and declared an array called $items that holds all the PHP types you can imagine -- booleans, strings, empty strings, integers, floats, null, arrays, empty arrays, and objects too. The rest of the code really chews up the current $item and spits it out for lunch... Try running this code so you can see the HTML TABLE that is created -- it'll be worth your while.
<?php
class Point { }
class Dimension { }
class Rectangle { }
$items = array(true, false, null, 23, 0, -26, 4.21, 0.0, -3.76,
'hello', '', array(1, 2, 3), array('', '', ''), array(),
new stdClass(), new Point(), new Dimension(), new Rectangle());
echo '<table cellpadding="4" border="1">
<tr>
<th>syntax</th>
<th>value</th>
<th>type</th>
<th>empty</th>
<th>boolean</th>
</tr>' . "\n";
foreach($items AS $item)
{
$booleanValue = (boolean)$item;
$empty = (empty($item) ? 'EMPTY' : ' ');
$type = gettype($item);
$syntax = 'if((boolean)';
$val;
if($type == boolean)
{
$val = ($booleanValue ? 'true' : 'false');
$syntax .= ($val . ')');
}
else if($type == 'NULL')
{
$val = 'null';
$syntax .= 'null)';
}
else if($type == double && !$booleanValue)
{
$val = '0.0';
$syntax .= '0.0)';
}
else if($type == string)
{
$val = '\'' . $item . '\'';
$syntax .= ($val . ')');
}
else if($type == 'array')
{
$val = $item;
$syntax .= '$array)';
}
else if($type == 'object')
{
$val = get_class($item);
$syntax .= ('$' . strtolower($val) . ')');
}
else
{
$val = $item;
$syntax .= ($val . ')');
}
echo ' <tr style="color: ' . ($booleanValue ? '#006600' : '#880000') . ';">
<td><code>' . $syntax . '</code></td>
<td>' . $val . '</td>
<td>' . $type . '</td>
<td>' . $empty . '</td>
<td>' . ($booleanValue ? 'TRUE' : 'FALSE') . '</td>
</tr>' . "\n";
}
echo '</table>' . "\n";
?>
Looking at the HTML output: notice that even integers and floats with a value of zero are considered EMPTY, and all that are EMPTY are FALSE boolean values. And take a gander at the boolean type with a false value... somebody is covering there bases!
Control Structures
Cuprins
Jeffrey
23-May-2012 01:28
wintermute
29-Aug-2007 12:45
Sinured: You can do the same thing with logical OR; if the first test is true, the second will never be executed.
<?PHP
if (empty($user_id) || in_array($user_id, $banned_list))
{
exit();
}
?>
Sinured
01-Aug-2007 11:59
As mentioned below, PHP stops evaluating expressions as soon as the result is clear. So a nice shortcut for if-statements is logical AND -- if the left expression is false, then the right expression can’t possibly change the result anymore, so it’s not executed.
<?php
/* defines MYAPP_DIR if not already defined */
if (!defined('MYAPP_DIR')) {
define('MYAPP_DIR', dirname(getcwd()));
}
/* the same */
!defined('MYAPP_DIR') && define('MYAPP_DIR', dirname(getcwd()));
?>
dougnoel
05-May-2006 06: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.
niels dot laukens at tijd dot com
26-Dec-2004 07: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
?>
