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

2.2.3     Output Control

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

PHP offers a great deal of flexibility as to how you want to output your content. In general use, PHP is embedded inside HTML in code islands started with <?php and ended with ?>.

You can reverse this by writing your whole script as one big PHP code island and printing HTML as necessary. Going back to the example shown previously, we can make our PHP code look almost identical to the Perl code by printing the HTML from inside our PHP code:

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

"Print" is a simple function that outputs a chunk of text, enclosed in quotation marks, to the client. "\n" means "start new line in the source code", and it serves to lay the source code out nicely. For the longest time, a debate raged on messageboards and mailing lists as to whether it was faster to drop out of "PHP mode" to output large amounts of HTML, or whether it was just as fast to stay in PHP mode. The truth is that it is horses for courses - you will find little or no speed difference either way.

PHP purists like to point out that print is technically not a function, and, technically, they are correct. This is why print doesn't require brackets around the data you pass to it. Other language constructs that masquerade as functions (and are referred to as functions herein for the sake of sanity) include echo, include, require, and exit.

PHP also has great output buffering features that further increase your control over the flow of output. An output buffer can be thought of as a storage hole where you can queue up content for outputting. Once you start a buffer, any output you create is automatically put into that buffer, and is not seen unless the buffer is closed and flushed - that is, sent to your visitor.

The advantage to this output queuing is two-fold. First, it allows you to clean the buffer if you decide not to output the current output queue in the buffer. When a buffer is cleaned, all the output stored in there is deleted as if it were never there, and output for that buffer is started from scratch.

Secondly, output buffering allows you to break the traditional ordering of web pages - that of headers first and content later. Owing to the fact that you queue up all your output, you can send content first, then headers, then more content, then finally flush the buffer. PHP internally rearranges the buffer so that headers come before content.

Output buffering is covered in much more depth later.





<< 2.2.2 Interpreting vs. Compiling   2.2.4 Performance >>
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 - 06 Sep 2008

that's a good simply tutorial.

A Fortran77 User ;o) - 06 Sep 2008

There is a little bit more to know about print and echo. The former returns a value whereas the latter does not.

Here some information taken from: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40


[Quote]
What is the difference between echo and print?
Which is faster, echo or print?

Feb 4th, 2004 01:20
geozipp, Nathan Wallace
Rasmus Lerdorf


There is a difference between the two, but speed-wise it
should be irrelevant which one you use. print() behaves
like a function in that you can do:

$ret = print "Hello World";

And $ret will be 1

That means that print can be used as part of a more complex
expression where echo cannot. print is also part of the
precedence table which it needs to be if it is to be used
within a complex expression. It is just about at the bottom
of the precendence list though. Only "," AND, OR and XOR
are lower.

echo is marginally faster since it doesn't set a return
value if you really want to get down to the nitty gritty.

If the grammar is:

echo expression [, expression[, expression] ... ]

Then

echo ( expression, expression )

is not valid. ( expression ) reduces to just an expression
so this would be valid:

echo ("howdy"),("partner");

but you would simply write this as:

echo "howdy","partner";

if you wanted to use two expression. Putting the brackets (which are
parentheses, not brackets) in there serves no
purpose since there is no operator
precendence issue with a single expression like that.

[/Quote]

Hope this helps a bit.

Luud Heck

A Fortran77 User ;o) - 06 Sep 2008

Oops, pity I cannot change my comment...

Of course I meant:

"... print is an alias for echo ..."

Cheers,
Luud Heck

A Fortran77 User ;o) - 06 Sep 2008

Note that printf is indeed an alias for print, however printf (note the "f") and friends are functions and do need the brackets.

Luud Heck

A Fortran77 User ;o) - 06 Sep 2008

Note that printf is indeed an alias for print, however printf (note the "f") and friends are functions and do need the brackets.

Luud Heck



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 nine plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow