Team LiB
Previous Section Next Section

Code Cohabitation

In the previous section, 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.

  1. Open a new file in your text editor.

  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.

    Click To expand

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

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

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.

The Importance of the Instruction Terminator

The instruction terminator, 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.

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

  7. Open your web browser and type http://127.0.0.1/errorscript.php.

What a nasty error! The error message says that the error is on line 8. Take a look at lines 7 and 8 of the script:

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.

Click To expand

This error is easy enough to fix:

  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.

  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.

    Click To expand

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!

Click To expand

Team LiB
Previous Section Next Section