Skip to main content
IBM  
Shop Support Downloads
IBM Home Products Consulting Industries News About IBM
IBM developerWorks : Web Architecture : Education - Tutorials
Writing efficient PHP
ZIPPDF (letter)PDF (A4)e-mail
Main menuSection menuFeedbackPreviousNext
2. Writing efficient code
  


Handling special cases early page 11 of 15


If your code includes any special cases where, for example, no action needs to occur in a function or method, place them as early in the code as possible and order them from most-likely-to-occur to least- likely-to-occur.

For example, in the function ConvertToUpperCase shown below, note that the test for an empty string is coded first. Then error conditions, such as the end index being less than 0 or the end index being less than the start index, are coded after that.


function ConvertToUpperCase( $str, $start, $end )
   {
   if ( $str == "" )
      return "";

   if ( $end < 0  ||  $end < $start )
      return $str;

   if ( $start < 0 )
      $start = 0;

   $len = strlen( $str );
   if ( $end > $len )
      $end = $len;

   return substr($str,0,$start)
          . strtoupper(substr($str,$start,$end+1-$start))
          . substr($str,$end+1);
   }

Main menuSection menuFeedbackPreviousNext
Privacy Legal Contact