note 88931 deleted from control-structures.alternative-syntax by danbrown
| From: | danbrown@php.net | Date: | Thu, 14 May 2009 16:48:12 +0000 |
| Subject: | note 88931 deleted from control-structures.alternative-syntax by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154605@lists.php.net to get a copy of this message | ||
Note Submitter: markandrewslade at dontspamemeat dot gmail
----
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.