Put PHP code inside other HTML tags

What we’re doing in this task:

We’re integrating PHP-generated content even more tightly into the HTML content of the page, and learning the power of re-using variables.

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

2. We want to include the user’s name in the welcome statement, so change the heading to look like this:

Welcome to My Simple Page, User!

3. Go to Code view.

4. Select the entire second PHP tag (echo $user), and Edit > Cut.

5. In the heading, select the word User and delete it; then paste the PHP code in its place.

The result should look like this:

<h1>Welcome to My Simple Page, <?php echo $user ?>!</h1>

6. Upload and view the page … check the source code.

Back in Dreamweaver … we can use this code anywhere:

7. Go to Code view

8. In the <title> element, we want the page title to be “George’s Simple Page” … so change the code to insert the php tags …

<title>George’s Simple Page</title>

<title><?php echo $user ?>’s Simple Page</title>

9. Upload and preview again

Why did we do this?

We learned that php tags and instructions can be inserted anywhere within the HTML, in place of static data.

We learned the value of defining information at the top of the page, and then using it multiple times in the page.