chapter 4 mixing php and html in this chapter, you’ll learn how to do the following: -recognize...

23
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

Upload: harriet-oliver

Post on 24-Dec-2015

220 views

Category:

Documents


6 download

TRANSCRIPT

Page 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

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

Page 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

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

Page 3: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 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

Page 4: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 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.

Page 5: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 5

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

Page 6: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 6

1. Open a new file in your text editor.

2. 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.

Page 7: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 7

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>:

Page 8: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 8

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).

Page 9: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 9

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:<HTML><HEAD><TITLE>My First PHP Script</TITLE></HEAD><BODY>

Page 10: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 10

3. Type the following PHP code:<?phpecho "<P><em>Hello World! I am using PHP</P></em>";?>

4. Add some more HTML so that the document is valid:</BODY></HTML>

Page 11: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 11

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.

Page 12: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 12

8. In your Web browser, view the source of this document(see Figure 4.4).

Page 13: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 13

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:<HTML><HEAD><TITLE>Making an Error</TITLE></HEAD><BODY>

Page 14: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 14

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.

Page 15: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 15

7. Open your Web browser and typehttp://127.0.0.1/errorscript.php. SeeFigure 4.5.

Page 16: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 16

Note: This error is easy enough to fix:

1. Open the errorscript.php file2. 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.

Page 17: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 17

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.

Page 18: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 18

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:

Page 19: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 19

<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>

Page 20: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 20

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.)

Page 21: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 21

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>";

Page 22: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 22

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.

Page 23: 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

Instructor: Mr. Nan Sokchea Tel: 097 9999 160 23

6. Commenting scriptsComments 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. */