Best Practices:PHP

These are best practice suggestions for you to use when making mods or doing any other PHP coding in general to give you faster and more optimized code.

Looping with variable size
When doing a loop which requires an element that has a variable size such as an array you need to fetch the size before running through the loop.  Incorrect  for ($i = 0; $i < sizeof($array); $i++) {   ... }  Correct  $size = sizeof($array); for ($i = 0; $i < $size; ++$i) {   ... }

/* OR */

for ($i = 0, $size = sizeof($array); $i < $size; ++$i) {   ... }

Incrementing
You should use local variables for incrementing when possible.

Declaring
You should declare all your variables before using them and remove any that are not being used.

Use global variables sparingly and always use the ones you define.

Incrementing
The use of ++ and -- before the variable on a incrementation is faster than after.  Incorrect  $i++; $j--;  Correct  ++$i; --$j;

Typecasting
Using a type casting functions is slower that using direct typecasting  Incorrect  $i = intval($value); $j = settype($var, 'float');  Correct  $i = (int) $value; $j = (float) $var;

Type Validation
Use the ctype functions when ever possible

Unset
Use unset to unset large array variables that are not longer needed unset($foo);

Strings
Use PHP string functions vs preg_match

Magic functions
The use of magic functions such as __get, __set, and __autoload should be avoided

_once
Using include_once and require_once is slower than include and require and should be using only in files where multiple inclusions might occur.

Error suppression
The use of @ for error should only be used when necessary.

Element of a result
Use the 'key' to fetch a result element  Incorrect  $row[value]  Correct  $row['value']