PHP Programming Tips


Variable Operations

  • Operating on an initialized variable is 376% faster than operating on an unitialized variable.
  • Constants are 146% slower than variables
  • Local variables are 9.9% faster than global variables

String Functions

  • ‘String’ is 0.26% faster than “String”
  • “String” is 4% faster than HEREDOC syntax
  • “String\n” is 108% faster than ‘String’.”\n”
  • ‘String’.$var is 28% faster than “String$var”
  • ‘string ‘.$var.’ string’ is 55% faster than sprintf(‘string %s string’, $var)
  • “\n” is 70% faster than chr(10)
  • strnatcmp() is 4.95% faster than strcmp()
  • strcasecmp() is 45% faster than preg_match()
  • strcasecmp() is 6.6% faster than strtoupper($string) == “STRING”
  • strcasecmp() is 13% faster than strnatcasecmp()
  • strtr($string, $string1, $string2) is 10% faster than str_replace()
  • str_replace() is 161% faster than strtr($string, $array)
  • stristr() is 10% faster than stripos()
  • strpos() is 9.7% faster than strstr()
  • isset($str{5}) is 176% faster than strlen($str) > 5
  • str_replace($str, $str, $str) twice is 17% faster than str_replace(array, array, string)
  • list() = explode() is 13% faster than substr($str, strpos($str))

Numeric Functions

  • ++$int is 10% faster than $int++
  • (float) is 48% faster than settype($var, ‘float’)

Array Functions

  • list() = $array; is 3.4% faster than assigning each variable
  • in_array() is 6% faster than array_search
  • isset($array[$key]) is 230% faster than array_key_exists()
  • !empty($array) is 66% faster than count($array)

Output Functions

  • echo is 5% faster than print()
  • echo ‘ ‘.’ ‘ is 0.44% faster than echo ‘ ‘,’ ‘
Advertisements

3 thoughts on “PHP Programming Tips

  1. I’m still learning from you, while I’m improving myself. I certainly love reading all that is written on your blog.Keep the tips coming. I liked it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s