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