| home / programming / phpanth1 / 1 | [previous] [next] |
|
|
PHP is a programming language that’s designed specifically for building Websites, 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 Website and experiment with the wealth of open source projects available through resources like HotScripts.
Unfortunately, the ease with which PHP-based sites can be developed also means 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 Websites. Of course, you can’t leap into programming and expect to know it all straight away. 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 Kevin Yank’s Build Your Own Database-Driven Website Using PHP & MySQL (ISBN 0-9579218-1-0), and instead concentrate on the essentials of “getting around” in PHP.
In this chapter, you’ll find out where to get help—a defence against those that bark “Read the manual!” at you—and how to deal with errors in your code. We’ll also discuss some general tips for keeping your code portable, and provide other essential roughage for your PHP diet. 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 the discussion of PHP syntax is not the most invigorating of subjects—although it is essential to prepare for later chapters. If you start to struggle, remember the line from The Karate Kid: you must learn “wax on, wax off” before you can perform the flying kick.
PHP is the most widely-used Web scripting language, running on over ten million domains around the world . 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 Websites, help is only ever a few clicks away.
There’s a well known four letter abbreviation, RTFM (I don’t think it needs explaining here), 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 make it their sole purpose to keep 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 down into five main sections plus appendices. It’s worth knowing what kind of information can be found, and where—at least within the first four sections, which are the most relevant to the typical PHP developer.
http://www.php.net/getting-started
This section provides a short introduction to PHP with some basic examples. It then explains how to install PHP (describing all sorts of operating system-Web server combinations), and how to configure it in terms of modifying your php.ini file.
Not to be overlooked is the section on security, which covers the areas in which PHP developers often make mistakes that leave their applications open to abuse. Once again, the “price” of PHP’s ease of use is that it won’t always protect you from your worst mistakes, so it’s worth getting started on security as early as possible in your PHP career. You’ll find a summary of key security issues in Appendix C, Security Checklist , as well as in discussions throughout this book, where appropriate.
This section covers the fundamentals of PHP as a programming language. Some of these are essential to your being able 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 is 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.
Covered here 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 developers point of view, the information contained here may be better understood when you see it in action—for instance, in the examples we’ll see throughout this book.
http://www.php.net/manual/en/funcref.php
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 the PHP group has made a concerted effort to make this section easy to get around. 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 down into subsections that cover various categories of functions, each category corresponding to a PHP extension.
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:
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.
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.
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.
| home / programming / phpanth1 / 1 | [previous] [next] |
Created: March 27, 2003
Revised: January 2, 2004
URL: http://webreference.com/programming/phpanth1