The Structure of a PHP Script

PHP is an embedded scripting language when used in web pages. This means that PHP code is embedded in HTML code. You use HTML tags to enclose the PHP language that you embed in your HTML file — the same way that you would use other HTML tags. You create and edit web pages containing PHP the same way that you create and edit regular HTML pages.

The PHP language statements are enclosed in PHP tags with the following form:

<?php      ?>

Sometimes you can use a shorter version of the PHP tags. You can try using <? and ?> without the php. If short tags are enabled, you can save a little typing. However, if you use short tags, your scripts won’t run if they’re moved to another web host where PHP short tags are not activated.

PHP processes all statements between the two PHP tags. After the PHP section is processed, it’s discarded. Or if the PHP statements produce output, the PHP section is replaced by the output. The browser doesn’t see the PHP section — the browser sees only its output, if there is any.

As an example, start with an HTML script that displays Hello World! in the browser window. (It’s a tradition that the first script you write in any language is the Hello World script. You might have written a Hello World script when you first learned HTML.)

<!doctype html>
<html>
<head><title>Hello World Script</title></head>
<body>
<p>Hello World!</p>
</body>
</html>

If you open this HTML script in your browser, you see a web page that displays

Hello World!

The following listing shows a PHP script that does the same thing — it displays Hello World! in a browser window.

<!doctype html>
<html>
<head><title>Hello World Script</title></head>
<body>
<?php
  echo "<p>Hello World!</p>\n";
?>
</body>
</html>

When you run this script, by looking at it in your browser, it displays the same web page as the HTML script. But now you're doing it with PHP!

Don’t look at the file directly with your browser. That is, don’t choose File→Open File from your browser menu to navigate to the file and click it. You must open the file by typing its URL in the browser’s address bar. If you see the PHP code displayed in the browser window instead of the output that you expect, you might not have started the file with its URL.

In this PHP script, the PHP section is

<?php
  echo "<p>Hello World!</p>";
?>

The PHP tags enclose only one statement — an echo statement. The echo statement is a PHP statement that you’ll use frequently. The output is simply the text that’s included between the double quotes.

When the PHP section is processed, it’s replaced with the output. In this case, the output is

<p>Hello World!</p>

If you replace the PHP section with the preceding output, the script now looks exactly like the HTML script. If you open either script in your browser, you see the same web page. If you look at the source code that the browser sees (in the browser, choose View→Source), you see the same source code listing for both scripts.

You can have as many PHP sections in a script as you need, with as many HTML sections as you need, including zero PHP or HTML sections. For instance, the following script has two PHP sections and two HTML sections:

<html>
<head><title>Hello World Script</title></head>
<body>
<?php
  echo "<p>Hello World!</p>";
?>
<p>This is HTML only.</p>
<?php
  echo "<p>Hello World again!</p>";
?>
<p> This is a second HTML section.</p>
</body>
</html>
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.