HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [197]
The first program you ever write in any language is invariably the “Hello World!” program or some variant thereof. Follow these steps:
1. Create a new PHP file in your editor.
I prefer using Komodo Edit because it already supports PHP, but if you add the PHP plugin to Aptana, that’s a great choice, too. (Read about these two programs in the earlier sidebar, “Picking a PHP editor.”)
If you’re using some other text editor, just open a plain text file however you normally do that (often File⇒New) and be sure to save it under htdocs with a .php extension. If you’re using a remote server, transfer your file to that server before testing.
2. Create your standard XHTML page.
PHP code is usually embedded into the context of an HTML page. Begin with your standard XHTML template. (See Book I, Chapter 2 for a refresher on XHTML.)
3. Enter the following code in the body:
print “
Hello World!
”;?>
Depending on your installation of Apache, you may be able to use the shorter ?> version of the PHP directive (instead of ). However, nearly all installations support the version, so that’s probably the safest way to go.
Note that you’re not just writing text, but creating an XHTML tag. PHP creates XHTML. That’s a really important idea.
4. Save the file.
Remember to save directly into htdocs or a subdirectory of htdocs. If you’re using a remote server, save remotely to that server (with Komodo) or save it locally and transfer it to the server to view it.
5. View the file in a Web browser, as shown in Figure 1-2.
The address of a Web page begins with the http:// protocol and then the server name. If the page is on the local machine, the server name is localhost, which corresponds directly to your htdocs directory. If you have a file named thing.php in the htdocs directory, the address would be http://localhost/thing.php. Likewise, if it’s in a subdirectory of htdocs called project, the address would be http://localhost/project/thing.php. If the page is on a remote server, the address will include the server’s name, like this:
http://www.myserver.com/thing.php
Figure 1-2: The “Hello World!” program example.
So, what is it that you’ve done here? You’ve figured out how to use the print statement. This allows you to spit out any text you want to the user.
Note that each line ends with a semicolon (;), just like JavaScript code. PHP is pretty fussy about semicolons, and if you forget, you’re likely to get a really strange error that can be hard to figure out.
Coding with Quotation Marks
There are many different ways to use print. The following are all legal ways to print text, but they have subtle differences:
print (“
Hello World!
”);print (“
Hello World!
Hello Computer!
”);print ‘
’;Any way you cut it, you have to have some form of quotations around text that you want printed. However, PHP is usually used to write XHTML code, and XHTML code contains a lot of quote marks itself. All those quotations can lead to headaches.
What if you want to print double quotation marks inside a print statement surrounded by double quotation marks? You escape them (you tell PHP to treat them as literal characters, rather than the end of the string) with a backslash, like this:
print “A Link”;
This can get tedious, so a better solution is discussed in the “Generating output with heredocs” section, later in this chapter.
This backslash technique works only with text encased inside double quotes. Single quotes tell PHP to take everything inside the quotes exactly as is. Double quotes give PHP permission to analyze the text for special characters, like escaped quotes (and variables, which you learn about in the next section of this chapter). Single quotes do not allow for this behavior, which is