note 88931 added to control-structures.alternative-syntax
| From: | markandrewslade at dontspamemeat dot gmail | Date: | Fri, 13 Feb 2009 19:01:32 +0000 |
| Subject: | note 88931 added to control-structures.alternative-syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150632@lists.php.net to get a copy of this message | ||
To mido_alone2001 at yahoo dot com,
Alternative syntax takes up more characters, so if you're looking to squeeze out that <
millisecond of processor time, using
<?php
if ($cond):
sleep(1);
endif;
?>
...is worse than...
<?php
if ($cond) {
sleep(1)
}
?>
Also, your method of embedding PHP inside of HTML instead of vice versa can be good, but it's
expensive. Consider these examples:
<?php
$variable = 'purple';
$iters = 10000000;
// Test 1
for ($x = 0; $x <= $iters; $x++) {
echo "<tr><td>$variable</td></tr>\n";
}
// Test 2
for ($x = 0; $x <= $iters; $x++) {
echo '<tr><td>'.$variable.'</td></tr>'."\n";
}
// Test 3
for ($x = 0; $x <= $iters; $x++) {
?><tr><td><?php echo $variable;?></td></tr><?php
}
?>
If you run these tests with some timing and reporting code in between, you'll see that test 3
performs *much* slower (approx. 30% slower over 10m iterations) due to the frequent hopping in and
out of PHP tags. Tests 1 and 2 are much closer in comparison. Here are some results I got
(redirecting to dev null and using STDERR to report):
me@jupiter scoping$ php scoping.php > /dev/null
Time passed for Test 1 over 10,000,000 iterations:
11.26
Time passed for Test 2 over 10,000,000 iterations:
11.20
Time passed for Test 3 over 10,000,000 iterations:
15.29
me@jupiter scoping$ php scoping.php > /dev/null
Time passed for Test 1 over 10,000,000 iterations:
10.88
Time passed for Test 2 over 10,000,000 iterations:
10.81
Time passed for Test 3 over 10,000,000 iterations:
14.88
Be wary of breaking out of PHP to render HTML within loops.
----
Server IP: 64.71.164.2
Probable Submitter: 168.122.64.150
----
Manual Page -- http://www.php.net/manual/en/control-structures.alternative-syntax.php
Edit -- https://master.php.net/note/edit/88931
Del: integrated -- https://master.php.net/note/delete/88931/integrated
Del: useless -- https://master.php.net/note/delete/88931/useless
Del: bad code -- https://master.php.net/note/delete/88931/bad+code
Del: spam -- https://master.php.net/note/delete/88931/spam
Del: non-english -- https://master.php.net/note/delete/88931/non-english
Del: in docs -- https://master.php.net/note/delete/88931/in+docs
Del: other reasons-- https://master.php.net/note/delete/88931
Reject -- https://master.php.net/note/reject/88931
Search -- https://master.php.net/manage/user-notes.php