Reduce the number of operations within loops whenever possible.
Here's a pointless-but-illustrative example.
<?php
$data = array(
'Santa Claus'
, 'Clark Kent'
, 'Barack Obama'
);
$displayAsUpperCase = true;
foreach ( $data as $datum )
{
if ( $displayAsUpperCase )
{
echo strtoupper( $datum ), "\n";
} else {
echo $datum, "\n";
}
}
See how the condition is executed every iteration? This is unnecessary, since the $displayAsUpperCase flag never changes during the lifetime of the loop. The solution is to use the value of the flag to define a callback
<?php
$data = array(
'Santa Claus'
, 'Clark Kent'
, 'Barack Obama'
);
$displayAsUpperCase = true;
$displayCallback = ( $displayAsUpperCase ) ? 'strtoupper' : create_function( '$data', 'return $data;' );
foreach ( $data as $datum )
{
echo call_user_func( $displayCallback, $datum ), "\n";
}
For those of you taking notes, this is a similar strategical process to polymorphism.