Part II: The Absolute Basics of Coding in PHP
Chapter 4: Mixing PHP and HTML
Overview
Now that you have a working development environment, with PHP, Apache, and MySQL happily running on your machine, it's time to delve into the PHP language. In this chapter, you'll learn how to do the following:
• Recognize and use the different kinds of PHP start and end tags
• Mingle PHP and HTML within your source code
• Escape special characters in your scripts to produce valid output How PHP Is Parsed
So you have a file, and in that file you have some HTML and some PHP code. This is how it all works, assuming a PHP document with an extension of .php.
1. The Web browser requests a document with a .php extension.
2. The Web server says, "Hey! Someone wants a PHP file. Something else needs to deal with it," and sends the request on to the PHP parser.
3. The PHP parser finds the requested file and scans it for PHP code.
4. When the PHP parser finds PHP code, it executes that code and places the resulting output (if any) into the place in the file formerly occupied by the code.
5. This new output file is sent back to the Web server.
6. The Web server sends it along to the Web browser.
7. The Web browser displays the output.
Because the PHP code is parsed by the server, this method of code execution is called server-side. When code is executed by the browser, such as
JavaScript, it is called client-side.
To combine PHP code with HTML, the PHP code must be set apart from the HTML. In the next section, you'll learn how this is done, using PHP start and end tags.
PHP Start and End Tags
The PHP parser recognizes a few different types of PHP start and end tags. It will attempt to execute anything between these tags, so it had better be valid code!
Study Table 4.1 to learn the three main sets of start and end tags recognized by the PHP parser.
Table 4.1: Basic PHP Start and End Tags Opening Tag Closing Tag
<?php ?>
<? ?>
Table 4.1: Basic PHP Start and End Tags Opening Tag Closing Tag
<script language="php"> </script>
Next, you'll use all three sets of tags in a script, which I promise will execute without errors.
1. Open a new file in your text editor.
2. Type the following code, which uses the first tag type:
3. <?php
4. echo "<P>This is a test using the first tag type.</P>";
5. ?>
6. Type the following code, which uses the second tag type:
7. <?
8. echo "<P>This is a test using the second tag type.</P>";
9. ?>
10. Type the following code, which uses the third tag type:
11. <script language="php">
12. echo "<P>This is a test using the third tag type.</P>";
13. </script>
14. Save the file with the name phptags.php.
15. Place this file in the document root of your Web server.
16. Open your Web browser and type http://127.0.0.1/phptags.php
Note While executing the examples in this book, if you are using PHP on an external Web server, substitute that server's domain name for 127.0.0.1 in the URL.
17. 18. In the next section, you'll learn that putting PHP blocks inside HTML is not a scary thing.
Code Cohabitation
In the previous section, your file consisted of three chunks of PHP code, each of which printed some HTML text. In this section, you'll create a script that has PHP code stuck in the middle of your HTML, and you'll learn how these two types of code can peacefully coexist.
1. Open a new file in your text editor.
2. Type the following HTML:
3. <HTML>
4. <HEAD>
5. <TITLE>My First PHP Script</TITLE>
6. </HEAD>
7. <BODY>
8. Type the following PHP code:
9. <?
10. echo "<P><em>Hello World! I'm using PHP!</em></P>";
11. ?>
12. Add some more HTML so that the document is valid:
13. </BODY>
14. </HTML>
15. Save the file with the name firstscript.php.
16. Place this file in the document root of your Web server.
17. Open your Web browser and type http://127.0.0.1/firstscript.php
18. In your Web browser, view the source of this document.
Notice that the HTML source contains only HTML code. This block of PHP was executed:
<?
echo "<P><em>Hello World! I'm using PHP!</em></P>";
?>
This block contains three elements: the command (echo), the string (<P><em>Hello World! I'm using PHP!</em></P>), and the instruction terminator (;).
Familiarize yourself now with echo, because it will likely be your most often-used command. The echo statement is used to output information—in this case, to print this HTML output:
<P><em>Hello World! I'm using PHP!</em></P>
The next section discusses a common error, with the hope that you'll be able to avoid it.
The Importance of the Instruction Terminator
The instruction terminator, also known as the semicolon (;), is absolutely required at the end of commands. The instruction terminator tells the PHP parser, "I'm done with this thing, try the next one."
If you do not end commands with a semicolon, the PHP parser will become confused, and your code will display errors. These next steps show you how to get one of these errors and, more importantly, how to fix it.
1. Open a new file in your text editor.
2. Type the following HTML:
3. <HTML>
4. <HEAD>
5. <TITLE>Making an Error</TITLE>
6. </HEAD>
7. <BODY>
8. Type the following PHP code:
9. <?
10. echo "<P>I am trying to produce an error</P>"
11. echo "<P>Was I successful?</P>";
12. ?>
13. Add some more HTML so that the document is valid:
14. </BODY>
15. </HTML>
16. Save the file with the name errorscript.php.
17. Place this file in the document root of your Web server.
18. Open your Web browser and type http://127.0.0.1/errorscript.php
echo "<P>I am trying to produce an error</P>"
echo "<P>Was I successful?</P>";
Line 8 does not have an instruction terminator, and line 9 starts a new command. The PHP parser doesn't like this, and it tells you so by producing the parse error.
This error is easy enough to fix:
1. Open the errorscript.php file.
2. On line 8, add the instruction terminator (;) to the end of the line:
3. echo "<P>I am trying to produce an error</P>";
4. Save the file.
5. Place this file in the document root of your Web server.
6. Open your Web browser and type http://127.0.0.1//errorscript.php
Escaping Your Code
Right up there with remembering to end your commands with semicolons is remembering to escape things such as quotation marks. When you use quotation marks inside other quotation marks, the inner pairs must be delineated from the outside pair using the escape (\) character (also known as a backslash).
The following steps show you what happens when your code isn't escaped, and how to fix it.
1. Open a new file in your text editor.
2. Type the following HTML:
3. <HTML>
4. <HEAD>
5. <TITLE>Trying For Another Error</TITLE>
6. </HEAD>
7. <BODY>
8. Type the following PHP code:
9. <?
10. echo "<P>I think this is really "cool"!</P>";
11. ?>
12. Add some more HTML so that the document is valid:
13. </BODY>
14. </HTML>
15. Save the file with the name errorscript2.php.
16. Place this file in the document root of your Web server.
17. Open your Web browser and type http://127.0.0.1/errorscript2.php Another parse error! Take a look at the PHP code:
echo "<P>I think this is really
"cool"!</P>";
Since you have a set of quotation marks within another set of quotation marks, that inner set has to be escaped.
This error also has a simple fix:
1. Open the errorscript2.php file.
2. On line 8, escape the inner quotation marks by placing a backslash before each one:
3. echo "<P>I think this is really \"cool\"!</P>";
4.
5. Save the file.
6. Place this file in the document root of your Web server.
7. Open your Web browser and type http://127.0.0.1/errorscript2.php
Now that the inner quotation marks are escaped, the PHP parser will skip right over them, knowing that these characters should just be printed and have no other meaning. In the next section, you'll learn a good programming practice:
commenting your code so people know what the heck is going on in it.
Commenting Your Code
Code commenting is a good habit to have. Entering comments in HTML documents helps you (and others who might have to edit your document later) keep track of areas of large documents. Commenting also allows you to write notes to yourself during the development process, or to comment out parts of code when you are testing your scripts.
HTML comments are ignored by the browser and are contained within <!-- and -->
tags. For example, the following comment reminds you that the code following it contains your logo graphic:
<!-- logo graphic goes here -->
PHP uses comments, too, which are ignored by the PHP parser. PHP comments are usually preceded by double slashes, like this:
// this is a comment in PHP code
But you can use other types of comments, such as
# This is shell-style style comment
and
/* This begins a C-style comment that runs onto two lines */
Create a script full of comments so that you can see how they're ignored. Yes, I'm telling you to write a script that does absolutely nothing!
1. Open a new file in your text editor.
2. Type the following HTML:
3. <HTML>
4. <HEAD>
5. <TITLE>Code Comments</TITLE>
6. </HEAD>
7. <BODY>
8. <!-- This is an HTML comment. -->
9. Type the following PHP code:
10. <?
11. // This is a simple PHP comment.
12. /* This is a C-style, multiline comment. You can make this as 13. long as you'd like. */
14. # Used to shells? Use this kind of comment.
15. ?>
16. Add some more HTML so that the document is valid:
17. </BODY>
18. </HTML>
19. Save the file with the name comments.php.
20. Place this file in the document root of your Web server.
21. Open your Web browser and type http://127.0.0.1/comments.php
You should see absolutely nothing in your Web browser, because all you did was print an HTML comment (which is ignored). Since PHP comments are ignored by the PHP parser, the PHP block didn't contain any actual
commands. If you view the source of this document in your Web browser, you will notice that only the HTML comment is visible. Although the PHP code was all comments, it was still parsed and is not visible to the end user.
HTML and PHP comments are used extensively throughout this book to explain blocks of code. Get used to reading comments, and try to pick up the habit of using them. Writing clean, bug-free code, with comments and white space, will make you popular among your developer peers because they won't have to work extra hard to figure out what your code is trying to do. In the next chapter, you'll learn all about variables, or, as I like to call them, "those things with the dollar signs."