With your first PHP script under your belt, you’ve made some real progress. But that PHP script might not have been what you expected. Most web developers don’t fall asleep at night dreaming of seeing this in a terminal

Hello there. So I hear you’re learning to be a PHP programmer!
Why don’t you type in your name for me:

Thanks, Brett, it’s really nice to meet you.

Even less impressive than its complexity (or lack thereof) is the script’s format. It’s just plain text. There’s no formatting; in other words, no H·TML.

In this chapter, you’re going to inject HTML into your scripts. No command-line prompts and boring text. By the time you’re through, your script will be speaking the language of the Web-HTML. In addition, you’ll see how PHP does one of its core tasks: respond to an HTML form .

script or HTML?

Before you can start doing fancy party tricks with PHP, you’ve got to get over a bit of a conceptual hurdle. 50 far in your web programming journey, you’re probably used to thinking about the technologies you’ve learned in strict categories: HTML is markup, the structure of your page; (55 applies style to that structure; and r Java5cript adds some interaction, with everything from alert boxes to validation, redirection, and widgets.

In the process, you probably also built some syntax categories. Your HTML is angle brackets, <title> and <head> and <ul>, and the like. (55 is curly braces and style keywords like p. warning and { } and border- style: dotted. The same is true with Javascript: you’ve got alert and strings in quotes like” Please enter a valid phone number. ” And those categories are distinct. Your HTML is separate from your (55, which is separate from your Java5cript, even though they all interact with one another

But with PHP, you’re going to have to abandon some of those categories. PHP happily-and sometimes confusingly-mixes these categories. You can write a PHP script that does programming tasks and then outputs HTML, (55, and even Javascript.

Determination by Extension

PHP scripts are identified by the extension .oiu» Accordingly, web servers that supports PHP see a file with a .pi1IJ extension and hand that file off to the PHP interpreter for processing. The interpreter does its thing and hands the result of the interpreted script back to the web server, which in turn passes that response along to a user’s oJ web browser. Another look at this process, which is shown in Figure 2-1,might help.

HTML CSS and Java5cript,

HTML CSS and Java5cript,

But what’s inside that script can be…well, all sorts of things. Remember, it’s the output of a PHP script that is ultimately handed off to a browser, so that response C”‘: t be PHP. It must be some combination of HTML, C55, and Java5cript-the things that a web browser knows how to handle.

In other words, a PHP script might be made up of PHP commands, but it also must be able to output more than just text, like S,lVI-;”I’C ~)hr) from Chapter 1does. It must be able to output HTML, C55, and Java5cript. Fortunately, this isn’t as difficult or tricky as it might sound

HTML Is Treated as HTML

You might be thinking, “Ok, I get it. I can use that echo command from Chapter 1 to output HTML, right?” Or maybe if you”ve used jQuery, you’re already a step beyond that: “Maybe there’s some cool PHP toolkit that makes building up an HTML and C55 response easy.” Although both of those thoughts are true, as you’ll see in this section, they’re actually not the simplest way to have a PHP script generate an HTML response.

For example, here’s some HTML for a simple web form, sort of like the program you already built

.<head> <link href=”css/phpW-l.css” rel=”stylesheet” type=”text/css” />
<div id=”header”><hl>PHP & My5QL: The Missing Manual</hl></div>
<div id=”example”>Example 2-1</div>
<div id=”content”>
<p>Hello there. So I hear you’re learning to be a PHP programmer!</p>
<p>Why don’t you type in your name for me:</p>
<form action=”scripts/sayHelloWeb.php” method=”P05T”>
<p><i>Enter your name:</i> <input type=”text” name=”name” size=”20″ /></p>
<p><input type=”submit” value=”5ay Hello” /></p>
<div id=”footer”></div>

Open the page locally on your own computer. (Check out the box on page 22 for more on getting your local web server going.)



PHP Is Not HTML (by Extension)

Just for the sake of experience, do something that might seem utterly bizarre to you: rename ~c nt,. -‘2~).r”i~) with a r’ extension, to <;a;t,W ,j’O~1 so.’)r;p. If you then double-click this file, a number of things might happen-none of which you want. If you have a code editor like Dreamweaver, xcode, or Eclipse, those editors might launch and show you your file. Or, you might get an error because your computer doesn’t know how to open the file

Even worse, if you open the file in your web browser (using the browser’s Open command, for example), the browser won’t know what to do with it. It will probably ask you if you want to save the file (as demonstrated in Figure 2-3).

opening say hello

opening say hello

The sr1y!if!l!oWeb.pilp file is definitely not HTML in terms of the file type. But the file contains HTML, so there must be some way to display that HTML. This time, instead of double-clicking the file or opening it with the browser’s Open command, type .the file’s URL directly into the browser’s address bar. (If you’re not sure what this URL is, refer back to the box on page 48.)

This time, you should see something that might surprise you; check out Figure 2-4 for the details



PHP Can Be HTML-by Response

As you learned in the previous section, the browser can’t handle reading a PHP script. but when you access the page through a locally running web server, things just work. That’s because the PHP interpreter is perfectly happy to take the HTML in the PHP script and push that HTML out as a response. The web server sends that HTML on to a browser, and this time-because the browser is getting HTML, not a file with ao.:o extension-it displays the HTML as a web page.

Now you’ve seen how a PHP script can return a full-blown HTML web page that any browser can display. Well, that’s actually what you’re going to be doing a lot in this book, starting in the next section: you’ll do some programming in your scripts, and return HTML as a response.

But first, rename sayHt’i!oV/eb.fJhp back to sayi-lelioWt>{J.litrm Then, look back at the form line in the HTML file:

<form action=”scripts/sayHelloWeb.php” method=”POST”>

This means that your form is going to submit its information to a program called ).pilp, a new PHP program you’re just about to write. (This time it will do more than just crank out HTML without any programming at all!) Once the form is submitted, sayilelioWel).php takes over, the PHP interpreter runs the code, sends out the response from <;ayl-!eifoWet) CPt:’, and hopefully that response is something a user;s web browser can understand and display.

PHP Talks Back

Now that you have an HTML page sending information to savHei!oWeb~)h!), you need to write some PHP. The PHP that you’re about to write to run on the Web is not that much different than the program from Chapter 1that you’ve already written. You have to get information a little differently because there’s no command line that a user can use to enter information. But other than that, things stay pretty much the same

Write Another PHP Script

Open a new text editor and type the PHP shown here; it should look sort of like an HTM~-ized version of the sd\’HE’!fi) ciu: program you’ve already written:

<link href= …./css/phpWo1.css ..rel=”stylesheet” type=”text/css” />
<div id·”header”><hl>PHP & MySQL: The Missing Manual</hl></div>
<div id·”example”>Example 2-1</div>
<div id-“content”>
<hl>Hello, <?php echo $_REQUEST[‘name’); ?></hl>
<p>Great to meet you. Welcome to the beginning of your
PHP programming odyssey.</p>
</div> .,
<div id-“footer”></div>

Save this program as sayHc/ioWen,.ohp within the scripts/subdirectory, and be sure that your file is in plain text and is using the right extension, .mu:

Once you realize that a lot of this script is just HTML, you can probably already guess what most of this program does. Here’s a section-by-section breakdown:
• The page starts out with a normal <html> element and head section.
• The body section begins and sets up the page heading and example number, just like the regular HTML page, snvnetlovveo.ntrn:
• The page defines a heading with <hi> and prints “Hello,”.
• The < 7php tells the browser some PHP code is coming. Then, the $_REQUEST II variable is accessed, and a property called namewithin that variable is printed by using echo.
• The end of the PHP code is indicated with 7>,
• The rest of the HTML is output, just as in sayHelioW0b.html.

This program, like most PHP programs you’ll write, accepts its input from a web page, either from one built in HTML like the pages you’ve created before, or from another PHP program. It’s the job of that web page-sayHelioWcb,htrn! in your case-to let the user enter information, and then send that information on to this program. The information from that HTML page is stored in $ REQUESTw, hich is a special variable in PHP,

Variables Vary

A in PHP (or any other programming language) is Simply a piece of code that stores a value. Variables have names, and in PHP, those names can be almost anything you want. You can tell that something is a variable in PHP because its name begins with a $. So, $myHeight is a variable called “myHeight,” and $_ REQUESTis a variable called “_REQUEST.”

Variables are not just names, either. They also have a value. So the value of $myHeight might be the number 68 (for 68 inches) or the text “68 inches.” In PHP, though, you’re not stuck with that value forever. You can change the value of a variable, which is where the word “variables” comes from: a variable venes, or changes.

In sevi=itc veb.oi.i: you’re using the special PHP variable $_REQUESTto get the user’s name, which she entered into the form you built in sDvl-ie!io\:VeL),t”ln’. PHP gives you the ability to get to anything a user entered into a form by using $_ REQUESTand the name of the form entry field-in this case, “name.” So, $_ REQUEST[ ‘name’] returns the information a user put into a web form, specifically into an input field called “name.” If the user also entered in her phone number, say into a form field called “ohonerturnber.v vou could get that value in PHP with $_ REQUEST[ ‘phoneNumber’ ].

Once your PHP program grabs the value from the “name” form field, it prints out that value by using echo, something you’ve already used in your first PHP program (page 38), That value is dropped right into the HTML that’s sent back to the browser- something you’ll want to check out for yourself by running your new program.

Check Things Out Locally

Because you should have MAMP or WampServer installed, you can check the files you’ve created so far on your own computer, although you’ll need to go through your local web server. Start a browser and visit sayHefioWeb.htrnfon httc:,/iccaft;ost’S888 (in Mac OS X, using the default MAMP Apache port) or localhost(in Windows, using the default installation),



50 far, you’ve got an HTML page, a PHP script, and some C55 and images that are used by both. But, unless you plan on parading your user base through your office or den and letting them use your computer, things are pretty limited. This script only works on your local machine, and that’s got to change.

Run PHP Scripts Remotely

It’s time to get your programs out to the masses (or at least your buddy a few cubes down who doesn’t believe you’re a real programmer). That means you need a iI0Sti;)g crovuier. often called an tntcr=e: Service Provui»: (15P) or web hosting company. All a hosting provider does is provide you with server space to house your web sites and applications; software to serve up your HTML and C55 and Java5cript-and now your PHP and My5QL; and some connection with the domain name service (DN5) so that people can access your site with a name like cooiPiJpSttes,mm instead of

Once you’ve got a hosting provider, it’s just a matter of getting the right connection information, and getting your files online. Finding a hosting provider that fits what you’re looking for is probably the hardest task; for some help on that tricky problem, check out the box below.

Once you’ve selected a provider, there are a couple of key bits of information that you’ll need: the hostname to which you can FTP (page 8) and connect via 55H or tel net, and the directories into which your web files should go. If you’re unfamiliar with connecting by using FTP or 55H, your hosting provider probably has some helpful tutorials on how to do all of this .

The Host with the Most

This is one of the toughest questions in the entire book. There are so many factors to consider, and everyone reading this has different priorities. Are you looking for an inexpensive solution or is stability and support at the top of your wish list? Will you use a gamut of technologies from PHPto Ruby on Rails to MySOLand PostgreSOL to Word Press to (offeeScript-or is HTML. (SS, JavaScripl, and PHP and MySOL enough? Do you want upgradeable server software and mailed-out logs and the.ilbility to configure (PUs and online backups, or is a simple SSH/telnet session enough for you? In the long run, only ,r’,know the answers to those questions. But, to work through this book, here’s what you absolutely need:
• PHP support (version 5 or higher) MySOL support (version 5 or higher, preferably 5.5 or higher) Some type of terminal access to your account, like telnet or SSH. Some type of FTPaccess to your account.

Once have your hosting provider set up, it’s time to upload some files.

Upload your HTML, C55, and PHP

When you’re building a web page, you have to upload your HTML, (55, and any Java5cript you’ve written to your own web server. Then, you access those files with a browser, through a web address Typing that web address into your browser causes your server to supply your HTML to whatever web browser requested the page

PHP works the same way. Once you’ve written your PHP programs, you upload them to your web’ server along with your HTML and (SS. Typically, you’ll end up with files and directories like this:

Root or Home Directory (/). This is your web root in which you put all of your HTML. This usually is the location referenced by a URL like .’leiiow[c3Qrnedid without any specific file-after the web server name.
CSSDirectory (css/). This is the directory in which all of your site’s (SS is stored.
JavaScript Directory Os/). Your JavaScript files go here. You’ll often see this directory also called sctiots/, but because PHP programs are also called scripts, it’s a good idea to be more explicit in your naming .

PHP Directory (scripts/). Here’s where you’ll put all of your PHP programs. Again, you could call this something more specific like Dilo/ or piipScripts/, but more often than not, websites use scrtots/ for this directory, so following that lead is a good habit to get into.

Examples Directory (ch01/, ch02/, and so forth). As you’re working through the examples, you’re going to end up with a lot of PHP programs, and fast. To k-eep everything organized, you should have a separate directory for each chapter. For example,

Now that you have your HTML and PHP ready, you need to upload those files to the appropriate directories on your web server. You should also download pilnMM.c;s as well as the accompanying images from the book’s website at

Once you have everything in place, your web server directory structure should look something like Figure 2-6 .

HTML and PHP files you created

HTML and PHP files you created

Run “Your Second Program

If you followed along in the previous section, you’ve got your HTML and CSS in their proper places, and your HTML form has your PHP program set as its action. You also should have sayi-i,>ito\V:;O.ptlP in your cN)2/scnpts/ directory. All that’s left is to take your PHP for a spin. Start a web browser, go to your web server, and then add ch02 :sayheJ!oWeV. nunt to your server name



Type your name and then click the Say Hello button. This cleverly labeled submit button sends your name as part of the form to the form’s action, which is your program. That program then runs on your web server. You should •
get a response back, similar to Figure 2-8.
This is the same form and response you saw back in Figure 2-5. Whether it’s on your own computer or a remote server, the web page looks the same .



Welcome to Programming

It might seem like you’ve done a lot of work just to have a web browser tell you your name. In fact, you could probably write the same program in JavaScript if you wanted. But now that you’ve created a few PHP programs, you can see how easy it is to write this sort of code

And before you know it, you’ll be doing a lot more than telling users their names. You’ll be talking to a database, doing advanced calculations, making decisions based on information the user gave you and what you have stored in a database, and more. But it all begins with a little HTML, a PHP program like the ones you’ve just written, and the directory structure you’ve put in place

PHP and Your Provider

As you’ve probably already realized, when it comes to running your scripts, the hardest part often isn’t the PHP. Instead, it’s figuring out where things go for your particular hosting provider.

Where do your web files go? directory, or som’ewhere else altogether? Usually, you’ll see either a, r or directory. Or, to be even safer, just call or email your hosting provider and ask them. They’ll have an easy, definitive answer to this Question.

Where do scripts go? In a special directory, or anywhere, as long as they have a .» extension? Most hosting providers let you drop PHPscripts anywhere you want, and the host’s web server will serve anything with a .j.!) extension via the PHP interpreter. But this is another Question for which your hosting provider should supply a clear answer

Fortunately, these are all issues that once you figure them out the first time, you usually don’t have to figure them out again. So, take the time now to ensure that you can run your PHPon your hosting provider. Even though it’s nice to have PHPrunning locally, it’s online that PHPreally shines, ~nd the same will be true for MySQLsoon. .Going forward, it will be assumed you’re running things online, as well. So, although you can use a tool like Dreamweaver, NuSphere,or Eclipseto edit your scripts locally, all the examples and instructions expect that you’re uploading and running things remotely, on a hosting provider.

Posted on January 11, 2016 in Installing PHP on Windows Without WAMP

Share the Story

Back to Top
Share This