Article
The PHP Anthology Volume 1, Chapter 1 - PHP Basics
PHP Extensions
The notion of an extension can be a little confusing to start with, as many are distributed with the standard PHP installation. The String functions, which we'd be pretty hard-pressed to live without, are a case in point. In general, the PHP group distributes, as part of the default PHP installation, all the extensions they regard as being essential to developers.
Extensions regarded as "non-essential" functionality (i.e. they will be required by some, but not all developers) must be added separately. The important information appears under the heading "Installation" on the main page for each extension. Core extensions are described with the sentence "There is no installation needed to use these functions; they are part of the PHP core." Nonstandard extensions are examined in Appendix B, Hosting Provider Checklist.
Access to information within the Function Reference is available through the search field (top right) and searching within the "Function List". Note that searching within the function list examines only the Function Reference section of the manual. To search the entire manual you need to search within "Online Documentation."
Another handy way to get around is to "short cut" to functions by passing the name of the topic you're interested in via the URL. For example, try entering the following in your browser's address field: http://www.php.net/strings. This will take you to http://www.php.net/manual/en/ref.strings.php, which is the main page for the Strings extension. Looking at this page, you'll see a list of all the functions made available by the extension; the same list is available in the menu on the left hand side.
Taking the strpos function as an example, enter the URL http://www.php.net/strpos (which takes you to http://www.php.net/manual/en/function.strpos.php). You will see the following information about the strpos function:
(PHP 3, PHP 4)
strpos -- Find position of first occurrence of a string
Description
int strpos (string haystack, string needle [, int offset])
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos(), this function can take a full string as the needle parameter and the entire string will be used.
If needle is not found, returns FALSE.
Line one contains the name of the function and line two lists the PHP versions in which the function is available. The third line tells you what the function actually does. In this case, it's a fairly terse explanation, but strpos really isn't a subject you can get excited about.
Under the Description heading is perhaps the most important line of all—the function's signature. This describes the arguments this function accepts and the value it returns in response. Reading from left to right, you have int, which tells you that the value returned by the function is an integer (in this case, the position of one piece of text within another). Next comes the name of the function itself, and then, in parentheses, the arguments this function takes, separated by commas.
Let's look at the argument string haystack. This says the first argument should be a string value, while haystack simply names the argument so that it can be referred to in the detailed description. Note that the third argument is placed inside square brackets, which means it's optional (i.e. you don't have to supply this argument).
Here's how you could use strpos:
Example 1.1. 1.php
<?php
$haystack = 'Hello World!';
$needle = 'orld';
// Use the strpos() function
$position = strpos($haystack, $needle);
echo 'The substring "' . $needle . '" in "' .
$haystack . '" begins at character ' . $position;
?>
Notice that I've used strpos similarly to the way it appears in the manual. I used the variable names $haystack and $needle to make clear the way each relates to the explanation in the manual, but you can use whatever variable names you like.
The function signature convention is used consistently throughout the manual, so once you're used to it, you'll be able to grasp quickly how to use functions you haven't tried before.
Get Help When Problems Arise
If you make a mistake using an in-built function in PHP 4.3.0, the default error reporting mechanism of PHP will display an error message with a link that takes you directly to the manual.
If you're ever in doubt, be sure to read through the comments submitted by other PHP developers, which appear at the bottom of every page in the manual. Usually, you will at least see an example of how the function is used, which may solve the particular dilemma you've run into. In many cases you'll also find alternative explanations and uses for a function, which help broaden your understanding.
Further Help
Outside the manual, there are literally thousands of online resources from which you can get further help. I would dare to say that 99% of all the common problems you'll encounter with PHP have already been answered somewhere, and are available online. That means the most obvious (but sometimes forgotten) place to begin is Google, where a quick search for "PHP strpos problem" will give you an idea of what I mean.
There are also some excellent sites where you can get answers directly from other PHP developers (for free, of course—it's part of the PHP ethic). Perhaps the three biggest in the English language are:
- SitePoint Forums: http://www.sitepointforums.com/
- Dev Shed Forums: http://forums.devshed.com/
- phpBuilder: http://www.phpbuilder.com/board/
Each of these uses vBulletin to host an online discussion and, as such, have very friendly and easy-to-use interfaces. All have very active memberships and you should find most questions answered within twenty-four hours.
Note that when you ask for help on forums, the principle of "helping others to help yourself" is important. Don't post a message that says, "This script has a problem" and paste in your entire PHP script. Narrow the problem down–identify the area where you're having problems and post this code snippet along with other relevant information, such as error messages, the purpose of the code, your operating system, and so on. People offering to help generally don't want to spend more than a few minutes on your problem (they're doing it for free, after all), so saving them time will improve your chance of getting a helpful answer.
Less convenient, but perhaps the most effective last resorts are the PHP mailing lists, where beginners are encouraged to use the PHP General list. The lists are available for limited browsing, though it's possible to search some of them using the search field from the PHP Website and selecting the list of your choice.
Zend, the company developing the core of the PHP engine, also hosts a fairly active forum for general PHP questions.
If you want to be guaranteed an answer, it's worth investigating PHP Helpdesk, a service run by Tap Internet, who have partnered with Zend to offer PHP training.