Entry
Can I escape from PHP code anywhere?
Can I mix code and HTML tags?
Jul 7th, 1999 21:21
Nathan Wallace, Mike Wood, Karl Pielorz, Rasmus Lerdorf
In PHP you can break out of script execution, insert HTML, and jump back
into the script from just about anywhere. By 'switching' off the
PHP3 engine for areas of static HTML your actually saving a lot on
processing / parsing, i.e. it's a very good thing...
if (mysql_num_rows($result) == 0) {
?><p><p>
<h2><center>Your search produced no results</center><p></h2>
<?
exit;
}
else {
$data = mysql_fetch_array($result);
?>
<p>
<table border=1 BGCOLOR=lightgrey>
<tr><td>field1</td>
<td>field2</td>
<td>field3</td>
<td>field4</td>
<td>field5</td></tr>
<?
print("<tr><td>$data[0]</td>
<td>$data[1]</td>
<td>$data[2]</td>
<td>$data[3]</td>
<td>$data[4]</td></tr>");
}
This is very much intended and encouraged. For often-used blocks you
can even do something like the following where we jump out of PHP mode
inside a function definition:
<? function block($arg) { ?>
Here is some straight <b>HTML</b>
And perhaps a <a href="<?echo $arg?>">Link</a>
<? } ?>
Now whenever you need that block of HTML just call block("whatever");