PHP Tutorials: Using Variables Within HTML

Lesson 2: Variables in HTML
Since we have already been show the basic principles to creating variables and how to display them within strictly PHP documents, it is now time to use these variable within html coding. However, unless you modify your htaccess file on your site, all files that contain any PHP coding need to have .php extensions.

Re-Using Your Variables Within HTML

Now you can open a standard HTML layout and leave all of the <html> tags such as <head><html><body>. You will be using your PHP tags before, and within, the HTML layout.

The example below shows how to display [echo] variables within your web pages. For this example, notice that you must close and re-open your PHP tags at various points [this way is more common with pages using PHP, <?php and ?>]

//create some variables:
$total 30+2;
$month "July";
$day "17th";
$year "1981";
<title>Example of PHP in HTML</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <td>The sum of thirty plus two equals: <?php echo $total?></td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <td>The month of my birthday is <?php echo $month?><br>
    The day of my birthday is <?php echo $day?><br>
    The year of my birthday is <?php echo $year?></td>


See How the PHP file will look in browser window

Look at the source code of the page [View >> Source]. Notice that, unlike the first lesson, the page contains HTML coding and echoes the variables within this coding.


Make Us Your Home Page

Add Us to Favorites

Tell a Friend About this Page

Open Index to this Issue