PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


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




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: