note 108750 added to language.control-structures

From: Date: Wed, 23 May 2012 01:28:04 +0000
Subject: note 108750 added to language.control-structures
Groups: php.notes 
Request: Send a blank email to php-notes+get-188143@lists.php.net to get a copy of this message
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' : '&nbsp;'); $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! ---- Server IP: 69.147.83.197 Probable Submitter: 66.81.159.182 ---- Manual Page -- http://www.php.net/manual/en/language.control-structures.php Edit -- https://master.php.net/note/edit/108750 Del: integrated -- https://master.php.net/note/delete/108750/integrated Del: useless -- https://master.php.net/note/delete/108750/useless Del: bad code -- https://master.php.net/note/delete/108750/bad+code Del: spam -- https://master.php.net/note/delete/108750/spam Del: non-english -- https://master.php.net/note/delete/108750/non-english Del: in docs -- https://master.php.net/note/delete/108750/in+docs Del: other reasons-- https://master.php.net/note/delete/108750 Reject -- https://master.php.net/note/reject/108750 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#188143) next »