Introducing PHP

PHP is a scripting language designed to be embedded into the HTML markup used for web pages. Web pages that contain PHP scripts are preprocessed by the PHP scripting engine and the source code replaced with the output of the script. Indeed, the acronym PHP suggests just that; PHP: Hypertext Preprocessor.

Consider a simple PHP script embedded in an HTML document:

<!DOCTYPE HTML PUBLIC
  "-//W3C//DTD HTML 4.0 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd" >
<html>
  <head>
     <title>Hello, world</title>
  </head>
<body>
  <?php echo "Hello, world"; ?>
</body>
</html>

When preprocessed by the PHP scripting engine, the short (and not very useful) script:

<?php echo "Hello, world"; ?>

is replaced with its output:

Hello, world

The text before and after the script is HTML; the first three lines define that HTML Version 4 is being used.

You can embed any number of PHP scripts in a single HTML document, as long as each PHP script is surrounded by the begin tag <?php and the end tag ?>. Other tags can also be used to delimit PHP scripts, but these are the most common and reliable.

One of the best language features of PHP is how it decodes user data and automatically initializes variables. Consider an example script stored in the file printuser.php:

<!DOCTYPE HTML PUBLIC
  "-//W3C//DTD HTML 4.0 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd" >
<html>
  <head>
    <title>Saying hello</title>
  </head>
<body>
  <?php 
    echo "Hello, $username"; 
  ?>
</body>
</html>

Let’s assume that the ...

Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.