A preview into Sam’s upcoming book series PHP: The Use Cases

5 reasons to maximize your use of PHP’s built-in features

“To truly get the best out of PHP you need to know as many of the built-in features as possible. Having trained many people and reviewed untold lines of code in my career, there is one thing I have said and have heard many many times. Don’t write yourself, what PHP already has a built-in function for!”

The quote above is from my upcoming book series PHP: The Use Cases. The goal of the series is to teach PHP developers about every single feature that is found in PHP. In this article I’m going to expand on why you would want to learn about all of the features.


If you were to re-create the functionality of one of PHP’s functions, in 99.9% of cases PHP’s built-in function would be faster. This is because the built-in functions are written in highly optimized C code. If you are not sure what C code is don’t worry, just trust me that it would be very hard to make it any faster. PHP has the reputation for not being the fastest language on the block. This has nothing to do with how well PHP is written, but everything to do with how flexible and dynamic PHP is.

When Facebook was looking to optimize the CPU usage of its PHP codebase, they thought they would be able to go into the PHP core and optimize it. They found there was nothing to optimize (this is why they created HipHop for PHP). PHP’s core and extensions are not written in PHP itself, they are written in C code. Once your code execution enters the land of C code, by calling a built-in function, then you are taking advantage of one of the fastest programming languages in common use. As an example, Zend Server has a page-caching feature that is written as a PHP extension. With the cache enabled, you can get a performance approaching that of static HTML pages.

There are some exceptions to this rule. There are cases where a native PHP solution would be faster than using a built-in equivalent. As an example, let’s say you wanted to find a value in an array. You would normally use array_search or in_array. These functions will find the given value by looping over the array values serially. If you had an array that was pre-sorted you could use a binary search to speed things up. PHP doesn’t have a native binary search so this is a case where, with a big enough array, native PHP is faster than the built-in functions.

The PHP community is always looking for ways to squeeze more performance out of PHP. It’s not a very well publicized fact but with every new feature-release of PHP it will be a little faster. As an example, the jump from the version of PHP 5.3 to PHP 5.4 will give you a 10-20% speed improvement and a 20-30% memory improvement.


Unsere Redaktion empfiehlt:

Relevante Beiträge

Meinungen zu diesem Beitrag

- Gib Deinen Standort ein -
- or -