Download presentation
Presentation is loading. Please wait.
Published byHarriet Oliver Modified over 9 years ago
1
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. Instructor: Mr. Nan Sokchea Tel: 097 9999 160 1
2
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. Instructor: Mr. Nan Sokchea Tel: 097 9999 160 2
3
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, which means this is a file that needs to be parsed,” and sends the request on to the PHP parser. 1. How PHP Is Parsed Instructor: Mr. Nan Sokchea Tel: 097 9999 1603
4
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. 4
5
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 5
6
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 1.Open a new file in your text editor. 2.Type the following code, which uses the first tag type: <?php echo " This is a test using the first tag type. "; ?> Figure 4.1 Your first PHP script. 6
7
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 3. Type the following code, which uses the second tag type: <? echo " This is a test using the second tag type. "; ?> 4. Type the following code, which uses the third tag type: echo " This is a test using the third tag type. "; : 7
8
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 http://127.0.0.1/phptag.php should see the results of your script (see Figure 4.2). 8
9
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. 1.Open a new file in your text editor. 2.Type the following HTML: My First PHP Script 9
10
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 3. Type the following PHP code: <?php echo " Hello World! I am using PHP "; ?> 4. Add some more HTML so that the document is valid: 10
11
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).http://127.0.0.1/ Figure 4.3 The firstscript.php script running. 11
12
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 8. In your Web browser, view the source of this document (see Figure 4.4). 12
13
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. 1.Open a new file in your text editor. 2. Type the following HTML: Making an Error 13
14
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 3. Type the following PHP code: <? echo " I am trying to produce an error " echo " Was I successful? "; ?> 4. Add some more HTML so that the document is valid: 5. Save the file with the name errorscript.php. 6. Place this file in the document root of your Web server. 14
15
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 7. Open your Web browser and type http://127.0.0.1/errorscript.php. http://127.0.0.1/errorscript.php. SeeFigure 4.5. 15
16
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 Note: 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 (see Figure 4.6):. echo “ I am trying to produce an error ”; Figure 4.6 The updated errorscript.php file. 16
17
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. 17
18
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. 1.Open a new file in your text editor. 2.Type the following HTML: 18
19
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 Trying For Another Error 3. Type the following PHP code: <? echo " I think this is really "cool"! "; ?> 4. Add some more HTML so that the document is valid : 19
20
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.) 20
21
Instructor: Mr. Nan Sokchea Tel: 097 9999 160 Note: Another parse error! Take a look at the PHP code: echo " I think this is really "cool"! "; 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 " I think this is really \"cool\"! "; 21
22
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. 22
23
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. */ 23
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.