Introduction in PHP Anthology. Where do I get help?
Introduction in PHP Anthology. Where do I get help?
PHP is a programming language that’s designed specifically for building web sites, and is both blessed and cursed with being remarkably easy to learn and use. Getting started is extremely simple. Before long, the typical beginner can put together a simple web site and experiment with the wealth of open source projects available through resources like SourceForge.
Unfortunately, the ease with which PHP-based sites can be developed also means that you can quickly get yourself into trouble. As traffic to your site increases — along with the demand for more features and greater complexity — it’s important to gain a more intimate understanding of PHP, and to research application designs and techniques that have proved successful on large web sites. Of course, you can’t leap into programming and expect to know it all straight away. And even if you could, where would be the fun in that?
In this first chapter, I’ll assume you’ve had a basic grounding in PHP, such as that provided in the first few chapters of SitePoint’s introductory PHP title Build Your Own Database Driven Website Using PHP & MySQL, and instead concentrate on the essentials of “getting around” in PHP.
In this chapter, you’ll find out where to get help — a defense against those that bark “Read the manual!” at you — and take a quick tour of PHP 5 object oriented syntax and concepts. Not everything here fits under the heading of “basic” — there may also be a few surprises in store for the more experienced PHP developers, so keep your eyes peeled! Be warned, though, that although the discussion of PHP syntax isn’t the most invigorating of subjects, it is essential to prepare for later chapters. If you start to struggle, remember the lesson from The Karate Kid: you must learn “wax on, wax off” before you can perform the crane kick.
Where do I get help?
PHP is the most widely-used web scripting language, running on over 20 million web sites. For an open source technology that lacks any corporate funding whatsoever, its popularity may seem inexplicable. Yet PHP’s success is no mystery; it has one of the most active and helpful online communities of any technology. Recent estimates place the number of PHP developers worldwide at around 500,000 and, given the nature of the medium, it’s fair to assume that a large proportion are active online. In other words, for developers of PHP-based web sites, help is only ever a few clicks away.
Solution
There are numerous PHP resources available on the Web today, not the least of which is the official PHP Manual.
RTFM: Read the Fine Manual
There’s a well-known, four-letter acronym, RTFM, which tends to be used to harass beginners in all areas of computing. While I can understand veterans might be unwilling to repeat endlessly the same, well-documented instructions, I think the basic assumption should be that we all know how to read the manual in the first place.
The documentation for PHP is excellent, and is maintained by volunteers who dedicate themselves to keeping it up to date, understandable, and relevant. The online version is extremely easy to navigate and contains further know-how in the form of annotations from developers across the globe. The manual is one of the areas in which PHP is truly exceptional; software houses like Sun and Microsoft still have a long way to go to provide this quality of material to developers working on their platforms. The manual is also available in twenty-four different languages but as you’re reading this book I’ll assume you’re happy with the English version of the manual. It’s broken into five main sections plus appendices. It’s worth knowing what kinds of information can be found where — at least within the first five sections, which are the most relevant to the typical PHP developer.
I. Getting Started and II. Installation and Configuration
■ http://www.php.net/getting-started/
These sections are where a true beginner starts. Section I has a basic introduction to PHP, explaining what PHP is and what it can do, as well as providing a simple tutorial to show how PHP works. Section II shows how to perform a basic installation of PHP on various operating systems in detail.
III. Language Reference
This section covers the fundamentals of PHP as a programming language. Some of these are essential to your ability to achieve anything with PHP, while others become useful as you look for ways to improve your technique. Reading the whole lot in one sitting may well be like reading a dictionary. Fortunately, it’s possible to absorb much of the information contained in the language reference by reading the wealth of tutorials available online, and examining the code that’s used in open source PHP applications. Certainly, as you read this book, I hope you’ll pick up a thing or two about getting the most out of PHP. However, it’s worth familiarizing yourself with the subjects contained in this section of the manual, and keeping them in the back of your mind for future reference.
IV. Security
■ http://www.php.net/security/
This is a very important chapter for beginners and experienced coders alike. Containing information on configuration settings, file system and database security, and general “good practices,” it’s a must for all coders. Most security problems stem from the code, not PHP itself, so being paranoid is a good thing for any coder! The earlier in your PHP coding experience you become familiar with this section of the documentation, the better. After all, it’s easier to learn a good habit than to break a bad one.
V. Features
■ http://www.php.net/features/
Covered in this section are the core elements of PHP that are generally focused on solving specific web-related problems. Much of the Features section reads like an “executive summary” and, from a developer’s point of view, the information contained here may be easier to understand when you see it in action — for instance, in the examples we’ll see throughout this book.
VI. Function Reference
This section makes up the real body of the manual, covering all aspects of the functionality available within PHP. This is where you’ll spend most of your time as you progress with PHP, so you’ll be glad to hear that the PHP group has made a concerted effort to make this section easy to use. It’s even fun, in an idle moment, just to trawl the manual and be amazed by all the things you can do with PHP. Yes, I did just describe reading a manual as “fun!” The function reference is broken into subsections that cover various categories of functions, each category corresponding to a PHP extension. Apart from the core language syntax, most parts of PHP are grouped into extensions representing discrete functionality.
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 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 it regards as being essential to developers. Extensions that are regarded as nonessential functionality (that is, they’ll be required by some, but not all developers) must be added separately. The important information about each installation appears under the Installation heading on the extension’s main page. Core extensions require no installation, as they’re included with the PHP core; the documentation clearly indicates whenever this is the case with the sentence, “There is no installation needed to use these functions; they are part of the PHP core.” Nonstandard extensions are found in the PECL Repository. 4 PECL, the PHP Extension Community Library, is a directory of all known PHP extensions. The process for finding and installing PECL extensions is explained in the Installation and Configuration section of the manual. Access to information within the Function Reference is available through the Search field (at the manual’s top right) and by 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 move around the manual is to take short cuts directly to functions by submitting 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. At the bottom of the page, you’ll see a list of all the functions that the extension makes available.
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’ll see the information shown in Figure
The first line contains the name of the function, while the second line lists the PHP versions in which the function is available. The third line tells us what the function actually does. In this case, it’s a fairly terse explanation, but strpos really isn’t a subject that many can get excited about. Under the Description heading is perhaps the most important line of all — the function’s signature. The signature describes the parameters — the required and optional values this function accepts — and the return value, which is the value we receive after the function has run. Reading from left to right, we have int, which tells us 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 parameters this function takes, separated by commas. Let’s look at the parameter string $haystack. This says that the first argument — the value supplied to a parameter — 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 (that is, you don’t have to supply this argument). Here’s a simple example that shows this function at work:
helloworld.php
<?php
$haystack = 'Hello World!';
$needle = 'orld';
$position = strpos($haystack, $needle);
echo 'The substring "' . $needle . '" in "' .
$haystack . '" begins at character ' . $position;
?>
Notice that here, 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 quickly be able to grasp how to use functions you haven’t tried before.
User Comments
You’ll find user-submitted comments at the bottom of each page in the manual. Usually, at the very least, you’ll see an example that shows how the function is used — information 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 to broaden your understanding. Just keep in mind that the user comments are not part of the official documentation, so some of the comments may not be entirely truthful. Usually, any that are factually dubious are later corrected and clarified.
Other Resources
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 faced by someone, somewhere, and their resolutions are available online. This means that the most obvious (but sometimes forgotten) place to begin looking 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 English-language resources are:
■ SitePoint Forums, at http://www.sitepointforums.com/
■ Dev Shed Forums, at http://forums.devshed.com/
■ phpBuilder, at http://www.phpbuilder.com/board/
Each of these sites hosts online discussions and, as such, has a very friendly, easyto-use interface. All have very active memberships and you should find most of your questions answered within 24 hours. Before you post a question or use one of the other methods listed later to find help, be sure to read How To Ask Questions The Smart Way to help you do the homework for your question. Note that when you ask for help on forums, the principle of helping others to help yourself comes to the fore. One of the most common mistakes that beginners make when posting a question is to post a message that says, “This script has a problem,” and paste your entire PHP script below. Instead, it’s much better to narrow the problem down — identify the area where you’re having problems, and paste in this code snippet, along with other relevant information such as error messages, the purpose of the code, your operating system, and so on. The people who offer 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 chances of receiving a helpful answer. Also available to those with an IRC (Internet Relay Chat) client are numerous IRC channels dedicated to PHP development. Two of the more popular channels are #php on efnet.org, and ##php on freenode.net. These resources can provide immediate assistance for your problem but, like the forums, the channels’ users will expect you to have done your homework beforehand. Less convenient, but perhaps the most effective last resorts are the PHP mailing lists, where beginners are encouraged to use the PHP General User list. The lists are available for limited browsing, though it’s possible to search some of them using the Search tool on the PHP web site, and selecting the list of your choice from the search results. Zend, the company developing the core of the PHP engine, also hosts a fairly active forum for general PHP questions. Of course, if you want a guaranteed answer, it’s worth investigating paid support options. More information can be found on the Zend Network web site.