230 likes | 255 Views
Chapter 4 Mixing PHP and HTML. 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.
E N D
Chapter 4 Mixing PHP and HTML • In this chapter, you’ll learnhow 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. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
Chapter 4 Mixing PHP and HTML • In this chapter, you’ll learnhow 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. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
1. 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. The Web browser requests a document with a .php extension. The Web server says, “Hey! Someone wants a PHP file, whichmeans this is a file that needs to be parsed,” and sends the request on to the PHP parser. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
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. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
2. PHP Start and End Tags The PHP parser recognizes a few types of PHP start and end tags. Table 4.1 Basic PHP Start and End Tags Instructor: Mr. Nan Sokchea Tel: 097 9999 160
Open a new file in your text editor. Type the following code, which uses the first tag type: <?php echo "<P>This is a test using the first tag type.</P>"; ?> Figure 4.1 Your first PHP script. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
3. Type the following code, which uses the second tag type: <? echo "<P>This is a test using the second tag type.</P>"; ?> • 4. Type the following code, which uses the third tag type: • <script language="php"> • echo "<P>This is a test using • the third tag type.</P>"; • </script>: Instructor: Mr. Nan Sokchea Tel: 097 9999 160
5. Save the file with the name phptags.php. 6. Place this file in the document root of your Web server. 7. Open your Web browser and type http://127.0.0.1/phptag.php. In your Web browser, you should see the results of your script (see Figure 4.2). Instructor: Mr. Nan Sokchea Tel: 097 9999 160
3. Code Cohabitation • 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. • Open a new file in your text editor. • Type the following HTML: • <HTML> • <HEAD> • <TITLE>My First PHP Script</TITLE> • </HEAD> • <BODY> Instructor: Mr. Nan Sokchea Tel: 097 9999 160
3. Type the following PHP code: • <?php • echo "<P><em>Hello World! I am using PHP</P></em>"; • ?> • 4. Add some more HTML so that the document is valid: • </BODY> • </HTML> Instructor: Mr. Nan Sokchea Tel: 097 9999 160
5. Save the file with the name firstscript.php. 6. Place this file in the document root of your Web server. 7. Open your Web browser and type http://127.0.0.1/firstscript.php. In your Web browser, you should see the results of your script (see Figure 4.3). Figure 4.3 The firstscript.php script running. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
8. In your Web browser, view the source of this document (see Figure 4.4). Instructor: Mr. Nan Sokchea Tel: 097 9999 160
4. The Importance of the Instruction Terminator • The instruction terminator, also known as the semicolon (;), is absolutely required at the end of commands. • Open a new file in your text editor. • 2. Type the following HTML: • <HTML> • <HEAD> • <TITLE>Making an Error</TITLE> • </HEAD> • <BODY> Instructor: Mr. Nan Sokchea Tel: 097 9999 160
3. Type the following PHP code: • <? • echo "<P>I am trying to produce an error</P>" • echo "<P>Was I successful?</P>"; • ?> • 4. Add some more HTML so that the document is valid: • </BODY> • </HTML> • 5. Save the file with the name errorscript.php. • 6. Place this file in the document root of your Web server. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
7. Open your Web browser and type http://127.0.0.1/errorscript.php. SeeFigure4.5. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
Note: This error is easy enough to fix: Open the errorscript.php file 2. On line 8, add the instruction terminator (;) to the end of the line (see Figure 4.6):. echo “<P>I am trying to produce an error</P>”; Figure 4.6 The updated errorscript.php file. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
3. Save the file. 4. Place this file in the document root of your Web server. 5. Open your Web browser and type http://127.0.0.1//errorscript.php. (See Figure 4.7) Figure 4.7 The updated errorscript running. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
5. Escaping Your Code 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. Open a new file in your text editor. Type the following HTML: Instructor: Mr. Nan Sokchea Tel: 097 9999 160
<HTML> • <HEAD> • <TITLE>Trying For Another Error</TITLE> • </HEAD> • <BODY> • 3. Type the following PHP code: • <? • echo "<P>I think this is really "cool"!</P>"; • ?> • 4. Add some more HTML so that the document is valid: • </BODY> • </HTML> Instructor: Mr. Nan Sokchea Tel: 097 9999 160
5. Save the file with the name errorscript2.php. 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. (See Figure 4.8.) Instructor: Mr. Nan Sokchea Tel: 097 9999 160
Note: Another parse error! Take a look at the PHP code: echo "<P>I think this is really "cool"!</P>"; Because 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 7, escape the inner quotation marks by placing a backslash before each one: echo "<P>I think this is really \"cool\"!</P>"; Instructor: Mr. Nan Sokchea Tel: 097 9999 160
5. Open your Web browser and type http://127.0.0.1/errorscript2.php. (See Figure 4.9) Figure 4.9 Fixed errorscript2 script running. Instructor: Mr. Nan Sokchea Tel: 097 9999 160
6. Commenting scripts Comments also allow you to write notes to yourself during the development process or comment out parts of code when you are testing your scripts, so the code is not executed. - Single-line comments // this is a comment and will be ignored by the PHP engine - Multiline comments /* This is a comment that stretches over several lines. It uses the same beginning and end markers as in CSS. */ Instructor: Mr. Nan Sokchea Tel: 097 9999 160