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

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.

The Importance of the Instruction Terminator at PHP

The instruction terminator at php, 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 command, 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 these errors come about and, more importantly, how to fix them.

The way of Instruction Terminator at PHP

1. Open a new file in your text editor.

2. Type the following HTML:

<HTML>
<HEAD>
<TITLE>Making an Error</TITLE>
</HEAD>
<BODY>

3. Type the following at 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.

Take a look at lines 7 and 8 of the script at php:

echo "<P>I am trying to produce an error</P>"
echo "<P>Was I successful?</P>";

Line 7 does not have an instruction terminator, and line 8 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 at php:

1. Open the errorscript.php file.

2. On line 7, add the instruction terminator (;) to the end of the line:

echo "<P>I am trying to produce
an error</P>";

3. Save the file as at php.

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.

After you fix line 7, the PHP parser can deal with the file, and the rest of the output is successful. Avoid this and other errors by paying close attention to things such as semicolons and, as you’ll learn in the next section, quotation marks at PHP!

PHP Code Cohabitation Tutorial

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.

Step By Step PHP Code Cohabitation Tutorial:

1. Open a new file in your text editor for PHP Code.
2. Type the following HTML:

<HTML>
<HEAD>
<TITLE>My First PHP Script</TITLE>
</HEAD>
<BODY>

3. Type the following PHP code:

<?
echo "<P><em>Hello World! I'm using PHP!</em></P>";
?>

4. Add some more HTML so that the document is valid:

</BODY>
</HTML>

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.

8. In your Web browser, view the source of this document.

Notice that the HTML source contains only HTML code, which is correct because this block of PHP Code was executed:

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 in PHP Code.