note 54424 added to control-structures.for

From: Date: Mon, 04 Jul 2005 07:55:34 +0000
Subject: note 54424 added to control-structures.for
Groups: php.notes 
Request: Send a blank email to php-notes+get-91391@lists.php.net to get a copy of this message
Actually, it seems calling echo inside a for expression is not allowed, at least as of PHP 5.0.4 (built: May 10 2005 09:56:44). Executing the said code results in the following error: Parse error: parse error, unexpected T_ECHO in /usr/local/iwcdns/webadmin/foo.php on line 2 However, using the print() function seems to work. From this, it seems sensible to think that PHP does not treat echo as a expression. The following will work as expected: <?php for ($i = 'a'; $i != "aa"; $i++, print $i); ?> (Note the use of the character notation 'a', instead of just a. Using the latter works, but is wrong, as explained in section "Why is $foo[bar] wrong?" of the array section of the manual (http://www.php.net/manual/en/language.types.array.php).) Also, bpgordon's further shortening is not exactly equivalent to mison's examples. In a for loop, the step expression (i.e. the third one) is executed _after_ the body of the loop. Therefore, a more accurate equivalent would be: <?php for ($i = 'a'; $i != "aa"; print $i, $i++); ?> And further shortening this: <?php for ($i = 'a'; $i != "aa"; print $i++); ?> ---- Manual Page -- http://www.php.net/manual/en/control-structures.for.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54424 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54424&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54424&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54424&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54424&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54424&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54424&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54424&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91391) next »