You’ve got PHP installed locally and you’ve got a text editor. Now all you need is an actual program. Start your text editor and type the following code, exactly as shown here:
echo “Hello there. So I hear you’re learning to be a PHP pl”ogrammer!\n”;
echo “Why don’t you type in your name for me:\n”;
$name ; trim(fgets(5TDIN));
-echo “\nThanks, ” . $name . “, it’5 really nice to meet you.\n\n”;
This program does just a few simple things:
• Identifies itself as PHP by using < ?php.
• Prints out a Welcome message by using the echo command.
• Asks the user for her name, again by using echo.
• Ge.ts the user’s name and stores it in something called $name.
• Says hello to the user by printing out a message that includes the information stored in $name.
• Finishes up with the ?> characters .
It’s okay if not much on this list makes sense yet. especially the weird line beginning with $name=. There are also some strange characters like \n and STDIN that you’ll learn about soon. But see if you can follow the plain-English words through the basic path: the opening < ?php, the printing, the request for the user’s name, another bit of printing, and the closing I>.
Now, save this program. Name it sayf-:eiIc’.r)!l”), and ensure that you add thatoj,1o extension! Otherwise, you’ll have a lot of problems down the line. Save the file some place handy, like on your desktop, your home directory, or a folder you’re using to keep a)1your PHP programs in as you’re learning.
Default to Plain Text
Most of the popular text editors let you change from rich text to plain text on a per-file basis, but they automatically start out in rich text mode. That can become a pain, so you might want to change the setup of your editor to always start out in plain-text mode For TextEdit on the Mac,open the Preferences menu. At the very top, under Format, select “Plain text” (as shown in Figure 1-29). In Windows, if you use Notepad, you avoid this entire issue, so you’ve got nothing to worry about.
Run Your First Program
What good is it to get all this code typed in if you can’t see if it works? This particular program isn’t ready to run on the Web yet; first you need to add something to it in your command-line terminal program, so go ahead and fire that up. If you’re on the Mac, you should open up Terminal. In Windows 7 or earlier, go to Windows Start+ Run and then run commandor cmdfrom the menu to get a command line. In Windows 8, at the start screen, press Windows key + Rand then type cmd(as sh;>wn in Figure 1-30).
Now, go to the directory in which you saved your program, You can do a directory listing with dir (in Windows) or Is (on the Mac) to ensure that you’re in the right directory. Once you’re in the right directory, type this into your command line:
This instructs the php program to run and gives it your program, sevueuo.ot«: as the script to run. In short order, you should see the welcome message you typed, and then the program asks you for your name. Type your name and press Enter. The program should then greet you, just as shown in Figure 1-31
But .Where’s That Web Server?
Before you take that well-deserved break, there’s one question left to answer. Remember way back to the discussion about a PHP interpreter interacting with a web server? All that business about PHP running locally or running remotely? Uploading files, web hosting providers; remember all that stuff? If not, Figure 1-32 should be a helpful refresher as to how PHP usually functions
50 what gives? You installed PHP locally and ran your script without problem, but a web browser wasn’t involved
The PHP Interpreter Is a Program You Can Run
The PHP interpreter that’s shown in Figure 1-32 is just a program, like dir or Is or which or anything else you can type into a command-line or terminal window. And just like those other programs, you can run it on your scripts manually. In fact, . that’s just what you did. You ran the PHP interpreter (php) on your script, because you installed Wampserver or, if you’re on a Mac, because php is already installed.
But, this sort of script-where all it does is output some text-is not the typical PHP script. It’s more of a “blow bubbles in the kiddie pool” script helpful to get started, but just the tiniest taste of what’s coming .
So, you don’t need a web browser or a web server. You just needed the PHP interpreter. Because of that, there’s no sense uploading your script and trying to find the PHP interpreter on your hosting provider, which requires shell access, which in turn might require calling up tech support and spending 20 minutes on the phone giving out maiden names and birthdates …in other words, it’s just not worth it.
But, the HTML Is Coming …
Keep those credentials handy, though, because in the next chapter, you v,:ji! start uploading your scripts. You’ll move beyond simply outputting text and begin to output HTML. You’ll take input from an HTML form and churn back out styled, .web-friendly responses. And, you’ll move from using just a local PHP installation to using a remote one.