The PHP Project Guide (2014)
12. PHP Documentation
The PHP documentation is an absolute goldmine when used properly. This doesn’t mean that you should open it up and use whatever you read, as some examples have been specifically created to demonstrate methods that shouldn’t be used. You’ll also find a host of user posted information that should not just be plucked out without proper research and thought.
12.1 Finding what you need
You’ll find everything you need within the PHP manual, but finding it can sometimes can be tricky. A Google search for what you need to do (e.g. split a string by a particular character) will usually return what you need as the website is indexed fairly well in Google. As an example, if you needed to know how to split a string by a particular character, you could search “PHP split string by character”. At the time I’m writing this, it currently returns the explode function within the PHP manual as the first result, which does indeed split a string by a particular character. Although successful, there is another function that splits by characters and regular expressions, preg_split. This may work better, for example if you needed to split by one or more spaces. The explode function will not let you do this, and therefore you’d be stuck if you were none the wiser about the preg_split function. PHP has introduced a ‘see also’ section to their manual which will allow you to explore other functions or topics that may be related to what you’re looking at. As you may have guessed, within the explode page the first ‘see also’ result is preg_split. It’s always worth browsing and checking out alternative or extended features before using what you’ve had returned as the first result.
12.2 Deprecated functions
Also known as ‘functions that shouldn’t be used any more, but can’t be removed from the language’, deprecated functions should be avoided and replaced where they have been previously used. Of course, these can’t simply be removed as an upgrade of PHP would mean that thousands of applications would simply cease to work. For this reason, they are deprecated and another function is introduced, or another function is recommended to be used. The PHP manual will tell you if a function has been deprecated and will give you the alternative use, so ensure you look out for this when looking in the manual.
Be aware that not everyone looks out for deprecated functions and keeps up to date with the manual, so for example, a recent blog post that uses ereg_replace (to replace content within a string using a regular expression) doesn’t mean that it should be used, as this was deprecated as of PHP 5.3. It’s good practice to look up every function you’re using when referencing code from another source, as this could leave you with speed or security problems within your application. Or, a replacement function could simply provide you with the functionality you need that wasn’t previously available.
Remember, PHP includes a list of deprecated functions with each release, so review this and check if you’re using them, and subsequently go ahead and replace them with newer functions.
When browsing the PHP manual, you may find user posted comments. These contains code posted by the public and although somewhat moderated, the code contained within the posts may not be ideal for your application or in general. When referencing any code or advice posted here, make sure you seek advice from elsewhere before simply using it. You could also get involved by actively participating in the feed!
You should also bear in mind that simply plucking functions from the manual may not suit the environment you’re working in, may not be available in the version of PHP installed on the server or may not work well within the code you’ve already written.
12.4 Browsing for help
It’s easy to browse for help with any popular programming language, and PHP is no exception. A quick search for almost any PHP related question will return vast amounts of resources with tips, code snippets and general advice. The problem with this (as with searching for any information online) is that anyone can post something, regardless of experience. When searching for help, always question and research any advice or code you’ve found and if you’re unsure, ask within a well-established community where you’re sure experts can give you advice.
One of the best ways to get help is to be part of a community where more than one person can offer advice. You may get several responses, but those who respond to you may question each other and discuss best practice, speed or security concerns. This means you get many points of views.
Actively participating in any community can be extremely rewarding and should help you learn a lot more about PHP and web development in general.