1.2. 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. For more on this process, see the sidebar "How the Web server processes PHP files."

As an example, start with an HTML script that displays Hello World! in the browser window, shown in Listing 1-1. (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.)

Example 1.1. The Hello World HTML Script
<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!

Listing 1-2 shows a PHP script that does the same thing — it displays Hello World! in a browser window.

Example 1.2. The Hello World PHP Script
<html>
<head><title>Hello World Script</title></head>
<body>
<?php
  echo "<p>Hello World!</p>"
?>
</body>
</html>

When you run this script, by looking at it in your browser, it displays the same Web page as the HTML script in Listing 1-1.

NOTE

Don't look at the file directly with your browser. That is, don't choose FileOpen 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 in Listing 1-2 with the preceding output, the script now looks exactly like the HTML script in Listing 1-1. 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 ViewSource), 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>This is HTML only.</p>
<?php
  echo "<p>Hello World again!</p>"
?>
<p> This is a second HTML section.</p>
</body>
</html>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.234