note 54424 deleted from control-structures.for by danbrown

From: Date: Mon, 07 Dec 2009 16:06:56 +0000
Subject: note 54424 deleted from control-structures.for by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163655@lists.php.net to get a copy of this message
Note Submitter: pmv at nipl dot net ---- 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++); ?>

« previous php.notes (#163655) next »