Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > Advantages of PHP Wish List | Report Bug | About Me ]

2.2.1     The HTML relationship

This is NOT the latest copy of this book; click here for the latest version.

When used to output HTML content, PHP is embedded inside HTML in code islands, as opposed to in Perl, where HTML code is embedded inside the Perl script. The most common way to open and close PHP code blocks is by <?php and ?>. Here is an example of a simple page, shown in Perl first then in PHP - don't worry about what the code means for now:

#!/usr/bin/perl
print "<HTML>\n";
print "<BODY>\n";
print "<P>Welcome, $Name</P>\n";
print "</BODY>\n";
print "</HTML>\n";

And now in PHP:

<HTML>
<BODY>
<P>Welcome, <?php print $Name; ?></P>
</BODY>
</HTML>

As you can see, the PHP version is only a line shorter, but infinitely much easier to read because the majority of the page is just HTML. Some modules for Perl (particularly CGI.pm) help, but PHP continues to have a big lead in terms of readability. If you really wanted to, you could write your PHP script like the Perl script: switch to PHP mode and print everything out from there. However, it tends to get messy - as you can see!

Apart from editing legibility, another advantage to having most of the page in straight HTML is that it makes editing with commercial IDEs possible, whereas products like Dreamweaver and FrontPage trash Perl's print statements.

One key advantage to using PHP as opposed to some other solutions is that PHP code is all executed at the server, with the client only receiving the results of the script. What this means is that users never see your PHP source code because they are never sent it - they only see what you want them to see.





<< 2.2 Advantages of PHP   2.2.2 Interpreting vs. Compiling >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 13 Oct 2008

here is something mush much simply to same effect... look,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>The HTML relationship - Practical PHP Programming</TITLE>
<LINK HREF="/phpbook/stylesheet.css" REL="stylesheet" TYPE="text/css" />
</HEAD>

<BODY BGCOLOR="#e5e5e5">

<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">

<TR><TD ROWSPAN="2" COLSPAN="2">

<TABLE BGCOLOR="white" WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0" STYLE="border: 1px ridge #aaaaaa;">

<TR>

<TD BACKGROUND="/phpbook/imgs/topbg.png" HEIGHT="140" ALIGN="LEFT"><IMG HEIGHT="140" SRC="/phpbook/imgs/toplogo.png" ALT="Hudzilla.org - the homepage of Paul Hudson" /></TD>

<TD BACKGROUND="/phpbook/imgs/topbg.png" HEIGHT="140" ALIGN="CENTER" VALIGN="bottom">

<FORM METHOD="POST" ACTION="/phpbook/search.php"> <INPUT TYPE="TEXT" NAME="Search" VALUE="Search" /> <INPUT TYPE="SUBMIT" VALUE="Go!" /></FORM>

</TD></TR>

<TR>

<TD CLASS="topbar" BACKGROUND="/phpbook/imgs/barshadow.png" ALIGN="left" STYLE="padding-left: 20px;"><A HREF="/phpbook/index.php" CLASS="location">Contents</A> &gt; <A CLASS="location" HREF="/phpbook/read.php/2_0_0">Introducing PHP</A> &gt; <A CLASS="location" HREF="/phpbook/read.php/2_2_0">Advantages of PHP</A> </TD>

<TD CLASS="topbar" BACKGROUND="/phpbook/imgs/barshadow.png" ALIGN="right" STYLE="padding-right: 20px;"><A HREF="/phpbook/wishlist.php" CLASS="toplink">Wish List</A> | <A HREF="/phpbook/bug.php" CLASS="toplink">Report Bug</A> | <A HREF="/phpbook/read.php/1_12_0" CLASS="toplink">About Me</A> ] </TD>

</TR>

<TR>

<TD COLSPAN="2" STYLE="padding-left: 15px; padding-right: 15px; padding-bottom: 15px; padding-top: 20px; border-top: 1px solid #dadada; background-color: white;">

<TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0">

<TR><TD VALIGN="TOP" STYLE="padding-right: 15px;">

<H2 CLASS="pagetitle">2.2.1&nbsp;&nbsp;&nbsp;&nbsp; The HTML relationship</H2>
<p>
When

A PHP User - 13 Oct 2008

It was an *example*, Perl peeps - no need to cry ;o)

pracphp@spam.trace.yp.cx - 13 Oct 2008

Or you could use a 'here' statement (below). May I suggest a better example as the Perl here has been written in a deliberately cumbersome manner.

(I'm not a Perl coder knocking PHP - I'm a Perl coder *learning* PHP :-)

#!/usr/bin/perl
print<<EOT;
<html>
<body>
<p>Welcome, $name</p>
</body>
</html>
EOT

Milan - 13 Oct 2008

It's a bad form to write HTML tags in all caps.

alfarid23@yahoo.com - 13 Oct 2008

There is no reason for all those print statements, and nobody somewhat good in perl writes code like that...
in perl one could write this like this:
print qq|
<HTML>
<BODY>
<P>Welcome, $Name </P>
</BODY>
</HTML>
|;
voilla...



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow