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

Print Subscribe to PHP Subscribe to Newsletters
PHP Foundations

Embedding PHP in HTML

05/03/2001

Today we will discuss how to use special syntax to apply conditionals and loops.

Embedding PHP in regular HTML

If you have been paying attention to our earlier articles, you hopefully have picked up on how PHP can be embedded into a regular HTML document. For example, we should already know that the following is an example of how PHP is embedded:

<html>
<head>
<title>My first PHP Page</title>
</head>
<body>
This is normal HTML code
<?php 
	
		// php code goes here
?>

Back into normal HTML

</body>
</html>

Further, we also learned how we could quickly output variables from PHP code without all of the hassle of an echo statement by doing the following:

<?=$variable?>

Today, we will extend our knowledge of embedded PHP by discussing how PHP can be used to control the flow of a web page through conditionals or repetition-control structures.

How it works

As mentioned in earlier articles, PHP will only process things that are enclosed within one of its valid code blocks (such as <?php and ?>). Because of this, PHP effectively ignores everything that it was not specifically told to process and can be used to our advantage. For example, what will the output from the following be?

<?php

    $var = 5;
  ?>

    $var = 10;<br />

The variable $var has a value of: 

<?=$var?><br />

Is this a valid script? Yes, the output would be the following:

    $var = 10;
    The variable $var has a value of: 5

Notice that with the second assignment of $var, when we attempt to change the value from 5 to 10, it has no effect because it is not enclosed within valid PHP code-block syntax. So, instead of being processed, it is simply displayed to the web browser.

Embedded conditionals

What if we wanted to display HTML only under specific conditions? For example, we only want PHP to display a navigation menu if the user is validated -- how could we do this? Using what we already know, we could simply put all the code for the HTML menu within echo statements (paying careful attention to add a backslash any time we need to output a quote for our HTML to the web browser) but this method can quickly become cumbersome and difficult to follow. Instead, to deal with situations like this, we will introduce a new flavor of our code-block syntax:

<?php if(conditions) { ?>
... HTML CODE ...
<?php } ?>

Although this may be confusing, remember how PHP will process this code. To start, it will evaluate the first line of a normal if statement and then begin a code block. Then, we turn off PHP parser and jump into normal HTML code (all of which PHP will simply output to the browser and ignore) until, finally, we return to PHP code and close our if statement. The result of this technique is a way for us to control regular and standard HTML with nearly no intrusion by PHP into the syntax. Although the above example works, a special syntax is provided for instances where PHP is being used simply to control the output of standard HTML code:

<?php if(conditions): ?>
... HTML CODE ...
<?php endif; ?>

This syntax is identical in function to the original example provided.

Other valid embedded syntax

Beyond simple if statements, most control structures provide an alternative syntax that allows us to embed PHP code within standard HTML quickly and easily. For example, below are definitions for our repetition statements while and for (starting with while):

<?php while(conditions) : ?>
... HTML CODE ...
<?php endwhile; ?>

And an identical syntax for an embedded for loop:

<?php for(init;conditions;increment) : ?>
... HTML CODE ...
<?php endfor; ?>

Pages: 1, 2

Next Pagearrow




Recommended for You

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:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • Partner Sites
  • makezine.com
  • makerfaire.com
  • craftzine.com
  • igniteshow.com
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com