PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Embedding PHP in HTML
Pages: 1, 2

Embedded code in action

Now that we have a feel of the syntax behind embedded control structures, let's look at a likely situation where our new knowledge could be put to use. What if we wanted to list, in an HTML table, all the numbers between 1 and 7 and determine under what circumstances our friend Billy could purchase a candy bar? Before we begin, we should get an idea of exactly how our HTML table should be constructed. For our purposes, we will be creating a table of the following form:

1234567
nonononoyesyesyes

Now, let's take a loop at the HTML behind this table:



<html>
<body>
<table>
<tr>
  <td align="center">1</td>
  <td align="center">2</td>
  <td align="center">3</td>
  <td align="center">4</td>
  <td align="center">5</td>
  <td align="center">6</td>
  <td align="center">7</td>
</tr>
<tr>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">yes</td>
  <td align="center">yes</td>
  <td align="center">yes</td>
</tr>
</table>
</body>
</html>

Notice that, when dealing with static HTML we have no flexibility whatsoever to alter the content of the table without manually changing it. What if we wanted to show a table of all the possibilities from 1 to 100? Again, accomplishing such a task by hand is a waste of time. However, with PHP, we can find a solution to our dilemma quickly and easily:

<html>
<body>
<table>
<tr>
<?php for($l = 1; $l <=7; $l++) : ?>
<td align="center"><?=$l?></td>
<?php endfor; ?>
        </tr>
        <tr>
            <?php for($l = 1; $l <=7; $l++) : ?>
            <td align="center">
<?php if($l >= 5)  { 
        
        echo "yes";
    
    } else {

        echo "no";

    }
?>
</td>
<?php endfor; ?>
        </tr>    
</table>
</body>
</html>

What have we done? Looking at this example, we start by simply outputting the basic HTML code to construct the web page and begin a table. Then, we use PHP to start a for loop to count from 1 to 7. Within this loop, we display the HTML code to first start a table cell, then display the variable we are counting with ($L) and finally the HTML to close the table cell.

Once this has been completed, we close our row and start a new row and repeat the same looping process. This time, however, instead of simply outputting the looped variable, we use a conditional statement to determine if it is greater than or equal to the value 5 in which case we output "yes" or "no" depending on the value of $L. Finally, we finish the HTML for our table and web page and the script ends.

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

The result? Exactly the same static HTML page as we originally constructed except now we have the flexibility to examine any range with no extra effort. It is recommended that you play around and get very familiar with this syntax because it will be used quite often in future articles and in everyday practice.

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.


Read more PHP Foundations columns.

Return to the PHP DevCenter.




Recommended for You

  1. Cover of PHP Cookbook
    PHP Cookbook
    Print: $44.99
    Ebook: $35.99
  2. Cover of PHP Anthology
    PHP Anthology
    Print: $39.95
  3. Cover of Web Database Applications with PHP and MySQL
    Web Database Applications with PHP and MySQL
    Print: $44.95
    Ebook: $35.99
  4. Cover of Head First PHP & MySQL
    Head First PHP & MySQL
    Print: $44.99
    Ebook: $35.99

Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

html

Articles that share the tag html:

Very Dynamic Web Interfaces (62 tags)

Rich Web Text Editing with Kupu (35 tags)

Analyzing HTML with Perl (25 tags)

Ajax on Rails (24 tags)

A Simpler Ajax Path (18 tags)

View All

webdesign

Articles that share the tag webdesign:

What Is Web 2.0 (229 tags)

Rolling with Ruby on Rails (95 tags)

From Weblog to CMS with WordPress (72 tags)

Very Dynamic Web Interfaces (53 tags)

Making Your RSS Feed Look Pretty in a Browser (52 tags)

View All

howto

Articles that share the tag howto:

Rolling with Ruby on Rails (258 tags)

From Weblog to CMS with WordPress (98 tags)

Top Ten Digital Photography Tips (92 tags)

Top Ten Mac OS X Tips for Unix Geeks (79 tags)

View All

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com