Escaping Your Code from php

Your Code from php Right up there with remembering to terminate your commands with semicolons is remembering to escape characters 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 from php.

1. Open a new file from php in your text editor.

2. From php Type the following HTML:

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

5. Save the file with the name errorscript2.php.

6. Place this file in the document root of your Web server.

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

1. Open the errorscript2.php file.

2. On line 9, escape the inner quotation marks by placing a backslash before each

echo "<P>I think this is really \"cool\"!</P>";

3. Save the file from php.

4. Place this file in the document root of your Web server.

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 from php: commenting your code so other people know what the heck is going on in it, should they have to edit it from php.