PHP is ultimately just text that is taken by your web server and turned into a slit of commands and information for your web browser. And because you’re just working in text, there’s not a lot you have to do to get going as a PHP programmer. You need to become familiar with PHP itself, and the best way to do that is to install PHP on your own computer as well as becoming familiar with how PHP runs on a remote web server.
Then, you need to run an actual script. Don’t worry; it’s amazingly easy to write your first program in PHP.Not only that, you’ll run your script. upload it to your web server and access your script with a web browser …and that’s all in the first two chapters!
Throughout the process, you’ll begin taking control. With PHP,you become an active participant in your web pages. PHP lets you listen carefully to your users and say something back. So get going; there’s no reason to leave your users with passive HTML pages any longer
PHP Comes in Two Flavors: Local and Remote
One of the most difficult things to get a handle on when it comes to PHP prl5gramming doesn’t have much to do with programming at all. It’s figuring out just how PHP runs, how it interacts with your web browser and web server, and why it’s not possible to just double-click a PHP file on your hard drive and see the script in that file run.
HTML and CSS Run Within a Web Browser
First, it’s worth thinking back to when you were a wee programmer, writing your first HTML page. You could save that page in a file, name that file with a ./liml extension, and boom-you had a web page. Double-click that file, and on most computers, you see that page open up in a web browser. That’s because just as acor file is connected to the Microsoft Word program, a ntrn! file is connected to a web browser (specifically, the browser you’ve chosen as the default on your computer). Figure 1-1should give you an idea.
If you keep thinking back, you probably added some styling to your HTML pages. Using the style attribute and <style></style> tags in your HTML document, you could change fonts, add striping to your table rows, and generally spice up other- wise boring text.
Then, at some point, some well-meaning web designer slapped your hand and insisted that you start writing all your CSS in external style sheets, and referencing those files in the head of your HTML, like this:
<link rel·”stylesheet” href·”styles/mysite.css” type·”text/css” />
You might even have a few style sheets for the benefit of people viewing your website on mobile devices or printing out a page
<link rel.”stylesheet” href·”styles/mysite.css” type·”text/css” media·”all” />
<link rel·”stylesheet” href.”styles/print.css” type·”text/css” media.”print” />
But you can still double-click that HTML file, and your browser knows what to do (see Figure 1-2). That’s because, once again, the web browser is completely capable of not just rendering HTML, but applying all those C55 styles to tl’re page, too. Again, no extra software needed
At this point, even though you’re using only two technologies-HTML and CSS-you need only a single program to handle those technologies: the web browser.
PHP Is Not Part of Your Browser
And here’s where things change from the easy, browser-centric view of the world. When you download a web browser, you get HTML, C55, and Java5cript, but you do t get PHP. PHP scripts-which you’ll soon be writing-have to be interpreted by the PHP interpreter program, called oro. And, you can’t just add a PHP interpreter to your browser. It doesn’t know what to do with scripts and isn’t built to interpret PHP.
Instead, you need PHP on a web server. It’s the web $0flf”I-not the web L)ro’o/,,,”- that can interact with a PHP interpreter. Your browser can handle HTML on its own, but it has to make a request to a web server to deal with PHP scripts. That server can take your PHP scripts and run them, and then take the response and send it back to your browser. Your browser can then understand and handle the response.
So, Figure 1-4 adds a couple of new wrinkles: the PHP interpreter, the magical thing that takes the PHP scripts you’ll be writing and does something useful with them; and a web server to communicate with that interpreter. These both live outside of your web browser. In this scenario, the browser now makes a request to the server and then takes the response and shows it to you
1. A web browser makes a request for some page. That page might be a URL on a remote web server, or a local file on your computer.
2. Assuming that the request goes to a web server, the web server returns HTML (and C55 and Java5cript).or, in the case of PHP, passes the PHP request on to the PHP interpreter.
3. The PHP interpreter does what it’s supposed to: it interprets, or runs, the PHP.The result of that should be something that a browser car1.understaod,like HTML. It passes this result, or response, back to the web server.
4. The web server gives the browser back something that the browser can understand: the HTML result of interpreting a PHP script, or C55, or Java5cript, or a combination of all of the above.
Understanding this difference in how PHP works, as opposed to HTML, C55, and Java5cript, is important because it determines the approach you’ll take to writing PHP scripts and getting those scripts to run.
Write Anywhere, Run Where There’s PHP
The cool thing about HTML, C55, and Java5cript is that because they’re built in to browsers and you can download browser-s so easily, those technologies become instantly available. It’s tough to even find a computer wit/wuta browser preinstalled. So, you turn on your computer for the first time, and boom, you can start creating web pages immediately. Double-click the HTML file, your browser fires up, and you’re good to go.
But PHP isn’t part of that browser. It’s not always preinstalled. If YOLl write a PHP script and then double-click it, you’ll probably see a code editor launch, but not something that will actually run that script. Even worse, if your browser does open up your PHP script, it’s not a web server. It doesn’t have a PHP interpreter. It will just show you your code, rather than run it, and what good is that to anyone?
This long prelude is just a big warning: although it’s easy enough to start writing PHP scripts, you can’t just open them in Dreamweaver or Fir~fox and expect them to run. You’ll end up frustrated and annoyed, and that’s no good for anyone .
2. You can write your scripts locally and always upload them to an Internet Service Provider (ISP) or web hos.tlng company. Every 15Pand web hosting company supports PHP, and you usually don’t have to do anything more than name your scripts with aphp extension. This option involves less initial setup, but it means that every time you edit your script, you need to upload it again to your 15P.It also means that double-clicking your PHP script won’t do aPlything-more than, at best, open your editor. You can’t test your scripts on your own computer.
Both choices are equally good, and which one you choose depends largely on your circumstances. Even though it might seem perfectly natural to jump right into uploading your scripts, you aren’t always qoinq to have a network connection. (The SOUM you just heard was the cheering of all the programmers who have an hour-long commute into work on their local metro or subway!) For those unwired situations, it’s nice to be able to keep developing on your own computer without the need to access your hosting provider. Note only that, installing PHP on your own computer is great for understanding what the PHP interpreter actually does.
So, before you start writing scripts that you can’t even run, it’s time to get PHP working on your own computer (if you want to), and then talk about getting scripts running out there in the wild, as well.
PHP on the Windows-Based Computers (Wamp Server Installation)
Open your favorite web browser and head to This is the online home of WAMP, which stands for Windows, Apache, MySQL, PHP. The site is shown in Figure 1-5.
Select the relevant Download link for your version of Windows. If you’re not sure, you can go to your Control Panel, select System, and then poke around. You’ll see either “32-bit Operating System” or “64-bit Operating System,” and that tells you what you want. Just select the first link on the top-left of the page that matches your system.
When downloading starts, you see a warning-actually, a couple of them-about needing some C++ extensions. Click the link for your system (see Figure 1-6), download the extensions (see Figure 1-7), and then run the downloaded file. You’ll need to allow the downloaded program to update your system, accept a license agreement, and install the extensions. When that’s complete, a screen appears like the one in Figure 1-8.
Once you’ve installed the C++ extensions, go back to select Downloads again, and then click the download link. This time, you can ignore the warning. Click the words “you can download it directly.”
The ad-heavy site you’re taken to will trigger a download in a few seconds. Then, save and run that file; you’re installing WampServer. Figure 1-9 is what you’re aiming for
Accept the license and default installation directory (typically C: ,wamp). You might want to create a Quick link icon, or at least a desktop shortcut. and then let installation take off. Select your default browser. You’ll then be asked about allowing Apache to access public networks (Figure HO). The best option here is usually the default supplied by the WampServer installer.
You then have a few other options for PHP mail, and then you’re finished. Launch WampServer, and you should see…nothing! Well, almost nothing. On the right side of the taskbar, notice there is now a little green “W” (check out Figure 1-11)
Click the green W icon to see all of the things you’ve been reading about, like PHP, MySQL, and Localhost, as shown in Figure 1-12
You’re almost done. Select the top option, Localhost. (If you don’t remember what local host means, see the box on page 22.) A new web browser window or tab opens with an address that references your own locally installed web server. This Server Configuration page presents information about your own web server setup (see Figure 1-13). It isn’t particularly impressive to look at, but it’s proof that your Windows computer can now serve up web pages.
While on the Server Configuration page, in the Tools section (about halfway down the page), click the phcntC() link. A page opens that looks something like Figure 1-14,which , is everything you’ll ever need to know about your local PHP installation.
More important, it means that your browser made a request to a web server, and that web server processed some PHP (the phpinfo function) and handed back a response to your browser. Not only CdI’ you run PHP on your computer, you just did
PHP on the Mac (Default Installation)
If you’ve got a Mac, you’ve got more than just a sleek, shiny machine and way too many ways to spend even more money with Apple, you’ve already got PHP installed. To prove it, open the Terminal application on your Mac. If you’ve never used Terminal, don’t worry; you’ll get used to it quickly and find it’s one of your best friends for working with PHP. Go to Aooltcaticns-eutilities-e Terminal.
Once you’ve found the Applications folder, open it and find the Terminal application. It looks like a computer monitor with a black screen and a little white arrow, as shown in Figure 1-15
To ensure that PHP is installed on your system, type php (all in lowercase letters) and press Enter. Unfortunately, the way to know things are working is if you con’t see anything but that blank cursor, a little further down in Terminal. It won’t even blink at you anymore; it’s just a boring, dark gray square.
Press Control-C to stop that single eye from hanging around and to display the blinking cursor again. This time, type which php. The which command lets you know where on your computer the program you type is located. In this case, you’re asking where the php program is located. You’ll probably get something back that looks like Figure 1-17;for the computer in this example, php is in the /usr/bin directory. You’ll probably get a similar result.
PHP on the Mac (MAMP Installation)
Although it’s nice that Macs come with PHP already installed, there might just be” a better option-one worth doing a bit of downloading and installing for yourself. That better option is MAMP, which stands for Mac, Apache, MySQL, PHP. This is the Mac counterpart to WAMP, the easy windows PHP installation (page 22) that you, as a Mac user, probably skipped.
MAMP doesn’t improve on the PHP installation that came on your Macs; it does integrate MySQL-which you’ll need before you know it-as well as the Apache web server and several helpful tools for working with PHP scripts and MySQL databases. You even get a simple control panel for starting up your local web server and MySQL database. Those additions are a nice perk, and coupled with how easy it is to install MAMP, you might just want to ditch the default PHP installation and get MAMP going.
Simply click the “Download now” button under MAMP and then grab a coffee and wait for the installer to download.
Now, launch the installer. Click Next a few times to select your hard drive and agree to the license. Keep going until the installer informs you that MAMP is ready to install, as shown in Figure 1-19.
Once the installation is complete, go to Applications-tMAMP. You’ll see a nifty control panel, a la Figure 1-20,
Your installation might try to automatically start both an Apache server and the database, Still, you can configure these easily by clicking the Preferences button. You should probably check the Ports tab and ensure that there aren’t any issues with any other software on your computer. You can do this all within MAMP, as shown in Figure 1-21.
You can also click the PHP Preferences option and see a few things that. honestly, probably don’t matter much to you (see Figure 1-22). Just leave these alone. In fact. there’s almost never a reason to mess with these selections. Mostly, it’s good to know that yes, MAMP did indeed install PHP (along with a web server and MySQL)
with just a few mouse clicks
There’s not much else to do now, so you can close Preferences and click the “Open start page” option to get a nice browser page like the one shown in Figure 1-23. Here’s where you’ll spend lots of your troubleshooting time as well as digging into databases once you’ve mastered the command line tools for MySQL that you’ll learn about
Before moving on, you can verify that this is all doing what it should. At the top of the MAMP start page, click the “pnplnfo” tab. A screen appears, similar to that in Figure 1-24. What’s significant here isn’t all the information listed; you needn’t concern yourself with that just yet. What is cool, though, is that you’re looking at a PHP script that’s been interpreted by a PHP interpreter (installed as part of MAMP).
The interpreter then fed the output of that script to your new MAMP-installed web server, which in turn handed that response to your web browser. Proof that you’re already running PHP
Get Out Your Text Editor
All the programs you’re going to write in PHP are plain, old text files. Writing PHP isn’t a lot different than writing HTML or (55 or Java5cript. You’ll type different things, of course, but these are all just text files saved with a special extension. You use for HTML,.L for (55, .> for Java5cript, and now you’ll use ‘h” for PHP files.
Because PHP is just text, you’ll want a good text editor in which to work. If you’re in Windows, you can use Notepad. As simple as that program is, it’s perfect for coding in PHP. If you’re on a Mac, TextEdit is a great choice. The good news is that each ‘of these programs comes preinstalled on your computer, so you don’t have to download or buy anything. The bad news is that none of these proqrarns «.«.
you’re writing PHP,so you don’t get much help if you type something wrong or want to organize your files without resorting to Windows Explorer or the Finder. These programs are simple, but limited.
On the other hand, there are quite a few editors out there that are built specifically to handle PHP. For instance, for Windows, you can download Nu5phere PhpED (“CiST) ‘,’re CC’fl”D. ciu([S”);’i,fJcf. “‘”Yj), which is shown in Figure 1-25. You’ll pay a bit for a program like Nu5phere-usually between $50 and $100-but you’ll get fancy
color coding ..help with special language features, and in a lot of cases, some nifty file organization features and the ability to upload your PHP directly to your web server
You can see what TextMate looks like in Figure 1-27. It’s a little sjrnpler than BBEdit, so if you’ve never used a programming editor, this might be easier to begin with . Textt-tate costs around $60, slightly less than BBEdit.
Text Editors: Mashing Up Programs
Although prnqrarnstlke PhpED,BBEdit, and TextMate are billed as text editors, they’re actually lots of programs rolled into one. Imagine having a text editor, a file management tool like f/indows Explorer or Finder, a tel net or terminal program, an FTPclient, and some glue to hold them all together. That’s more or less what these programs give you: a bunch of things all rolled into one single software package.
What’s great about these “text editors plus” is that they offer you all sorts of features, and you don’t need five or six icons in your Mac’s Dock or shortcuts on your Windows desktop. You have access to almost everything you’ll typically need to build web pages or program in PHP,right at your fingertips.
What’s not so great, though, is that generalized tools aren’t often as fully featured as specific tools. In other words, a program that tries to do everything usually does lots of things decently, as opposed to lots of programs that only do one thing, but do that one thing really well.
Much of the time, you’re making a choice between convenience and features. If you only use FTPto upload files to a server on occasion, you almost never work with your computer’s command line, and you get a kick out of colored editors, the bundled text editors with lots of extra features migllt be a good fit.
Whether you use a more full-featured text editor or not, thoucn, at some point you might need to ditch the editor and use an actual FTPortelnet program. As long as you’re comfortable diving into those programs “iI,,!’u! the use of an editor from time to time, by all means, go forth in code in TextMate or PhpED without worry.
Once you’re comfortable writing PHP code, you can spend some time playing with all these different enhanced editors. You can see what you like, discover whether an editor is perfect for you, or realize you’re a Notepad or TextEdit programmer at heart. There’s no one right option for PHP; all of these choices work just fine.
If you’re just starting out, though, try to use a simple text editor-Note pad on Windows or lextEdit on the Mac. You’ll learn a lot more about PHP this way, even if you don’t get all the bells and whistles of one of the full-featured editors. Besides, once you understand PHP and have learned to work with it manually, you’ll appreciate , and be able to use the features of the other editors a lot more effectively