Put PHP code above the HTML tag

What we’re doing in this task:

We’re adding PHP code to be processed before any of the HTML page is read.

We’re learning how Dreamweaver will structure the PHP code it writes for us.

1. In Dreamweaver, open simple.php and go to Code view.

2. Move the variable declaration to its own PHP tags, above the HTML tag. So the code for your page looks like this (PHP code is bold):

<?php

$user = “George”;

?>

<html>

<head>

<title>Simple Page</title>

</head>

<body>

<h1>Welcome to My Simple Page!</h1>

<?php echo $user ?>

</body>

</html>

3. Note that the variable declaration can be anywhere, as long as it’s before the echo statement that calls on the variable.

4. Note also that the echo statement can be displayed on one line, and without a semicolon.

This is perfectly allowable, and very simple statements are often written this way … whatever makes the code easiest to read, multiple lines or one line.

Because there’s only one statement, we can remove the semicolon also.

5. View the page in Design view. Note that the code above the HTML tag is not displayed anywhere here; just the code in the body tag.

6. Upload and view … the page should work the same as it did before.

Why did we do this?

We learned that pages can have as many PHP tags as they need.

We learned that the different sections of PHP code share information.

We learned that it’s perfectly valid — and common practice — to put PHP code entirely outside the HTML tags; things that will be used in the rest of the page are often placed here, so they’re sure to get processed before they’re used.

We learned that PHP tags can be “inline” or multiline.