Cacti (home)ForumsRepositoryDocumentation

PHP Specific Constructs

Traversing Arrays

Use the foreach ($array as $name ⇒ $value) syntax instead of while (list($name, $value) = each($array)) syntax when traversing arrays. The former has been demonstrated to be more efficient and does not increment the internal pointer of the array. The foreach construct will error if the array is empty or is not an array, so you must check this first.

if ((is_array($array)) && (sizeof($array) > 0)) {
	foreach ($array as $name => $value) {
		// code
	}
}

Error Return Values

When an a function needs to return a generic error, it is best to return boolean false in PHP. This makes it very straightforward to check for.

if (file_list() === false) {
	// there was an error
}

Regular Expressions Are Slow

Only use PHP's regular expression parsing functions (ereg, preg_match, etc) when absolutely needed. Often simpler functions such as strstr(), str_replace(), substr(), and explode() can be used, and are much faster.

$items = explode(":", $string));

Is must quicker than:

$items = split(":", $string));

Use ''preg'' Functions Instead of ''ereg''

The POSIX based regular expressions in PHP tend to be slower than their Perl-based equivalents. Use Perl-based regular expression functions unless there is an explicit reason to do otherwise.