Filed under: Everything >
Tech >
Web
Posted: Saturday, September 12, 2009
Getting Started in PHP
PHP is a powerful yet simple scripting language that typically runs on your website's server to produce HTML dynamically. This means that you can embed scripts directly in your HTML that process information in much the same way that standard programming languages like C do, but without having to install a custom executable on the web server (something hosting services are a tad reluctant to do). For a website developer, PHP provides the ability to make a website much more dynamic and useful for visitors, going far beyond what HTML can do by itself.
This kind of flexibility usually comes at a price, and in PHP's case, the price is having to run your scripts on a web server. For most designers, the server is a different machine from the one used for design, often located in a different city. It's a small inconvenience to have to upload the scripts you are developing to your server for testing, but if you multiply that little inconvenience by the hundreds of little changes a script might require, it starts to add up to some serious time.
Especially when you're first learning the language. Like I said, PHP looks a lot like C, so C programmers are going to have a headstart on the learning curve. However, there are some small but pervasive differences from C, such as the requirement that all variable names start with a dollar sign. Forgetting that little rule, along with the occasional missed semi-colon, etc., leads to many useless cycles of uploading, testing and editing, needlessly lengthening the time to get comfortable with PHP.
Fortunately, the developers of PHP have made it possible to install the PHP engine locally on a development machine and run scripts without needing a full web server. The PHP executable can take a filename on the command line (e.g., index.php) and then spit out the html that would be generated by the script in that file. Unless you have syntax errors, of course, in which case it tells you what line it is having trouble interpreting. This means that you can check the syntax of your script from the comfort of your development machine without ever having to make a remote connection to a server. Of course the command-line version of PHP isn't a full web server environment, so it won't catch errors in logic that depend on the server environment, but even doing simple syntax checking locally is a huge relief for the programming process, especially for beginners.
But wait, there's more. If a script can be run from a command line, it's not a far stretch to incorporate that run ability directly into an editor. That capability is exactly what you find in the Antechinus Javascript Editor, developed by C Point Pty Ltd. I'm sure there are other editors with this capability as well.
This was welcome news to me when I started learning PHP. Even though Antechinus is a Javascript editor, PHP files are fully supported, including this all-important integration with the PHP engine for syntax checking and the other usual editor goodies such as variable and function name prediction and context-sensitive help. The editor doesn't contain the PHP engine itself, however, so you have to download and install that on your system separately by visiting the PHP website. One word of warning: You will need to make some small edits to the php.ini file located in the main php directory for the installation to work properly. It is self-explanatory for the most part, but my failure to set one setting cost me a couple of hours of head-scratching. That setting is date.timezone in php.ini. By default it is unset, which causes the engine to output a large number of error messages every time you ask it to check a php file, which in turn totally confuses the syntax-checking process in the editor. Just set this to a reasonable value (e.g., date.timezone = 'America/New_York') and this problem should go away.
As with learning most web technologies, the problem with learning PHP is not a lack of information, it's finding a way to manage all of the available information. I usually get good advice from books by Wrox Press, and their Beginning PHP book covers all of the basics of the language that I care about. Maybe it's because of my advancing years, but I appreciate a book with a good table of contents and index, so that I can use it initially as a tutorial, and later as a reference. I had an earlier edition of the book than the one shown here, but the quality should be similar. That's not to say that there aren't some excellent resources on the web for learning PHP. Besides the documentation on the official PHP website, a good place to start is one of the tutorial websites. I like the PHP Tutorial from w3schools. I'm sure there are others. For more advanced discussions I like blogs, for example the PHP articles in the Electric Toolbox by Chris Hope.
PHP is a fascinating language, in that it contains a lot of the nuts and bolts of today's web development. Learning it really stimulates your imagination about the kinds of things that you can do to a website. I'm looking forward to some PHP projects for the plumbing on this website, and I'll try to write about them as I complete them. If you're just starting out, I hope these few links and pointers give you something to use to stimulate your own imagination. Enjoy.


