O'Reilly logo

Managing & Using MySQL, 2nd Edition by Hugh E. Williams, Randy Yarger, George Reese, Tim King

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required