note 65668 added to language.oop5.iterations

From: Date: Fri, 05 May 2006 13:46:29 +0000
Subject: note 65668 added to language.oop5.iterations
Groups: php.notes 
Request: Send a blank email to php-notes+get-110625@lists.php.net to get a copy of this message
The example code given for valid() will break if the array contains a FALSE value. This code prints out a single "bool(true)" and exits the loop when it gets to the FALSE: <?php $A = array(TRUE, FALSE, TRUE, TRUE); while(current($A) !== FALSE) { var_dump(current($A)); next($A); } ?> Instead, the key() function should be used, since it returns NULL only at the end of the array. This code displays all four elements and then exits: <?php $A = array(TRUE, FALSE, TRUE, TRUE); while(!is_null(key($A))) { var_dump(current($A)); next($A); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 69.205.153.76 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.iterations.php Edit -- http://master.php.net/note/edit/65668 Del: integrated -- http://master.php.net/note/delete/65668/integrated Del: useless -- http://master.php.net/note/delete/65668/useless Del: bad code -- http://master.php.net/note/delete/65668/bad+code Del: spam -- http://master.php.net/note/delete/65668/spam Del: non-english -- http://master.php.net/note/delete/65668/non-english Del: in docs -- http://master.php.net/note/delete/65668/in+docs Del: other reasons-- http://master.php.net/note/delete/65668 Reject -- http://master.php.net/note/reject/65668 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#110625) next »