Writing to the Browser PHP

Three functions in PHP will send text to the browser: echo, print, and printf. Each does the same thing: They take values and print them to the browser. The printf function allows you to specify the format of the output rather than sending values as-is. I've used print so far in my examples, mostly out of personal preference. I don't usually need the formatting that printf provides. Many older PHP examples you will find on the Web use echo because it existed in PHP2.

It is important to remember everything you write is in the context of a Web browser. Unless you take measures to make it otherwise, your output will be treated as HTML text. If you send text that is HTML code, it will be decoded by the browser into its intended orm. I've been sending <BR> via print throughout the book so far, but is a more dramatic example of this concept.

Sending HTML with print

<?
print("You're using ");
print($HTTP_USER_AGENT);
print(" to see this page.<BR> ");
?>

Of course, anything outside PHP tags is sent directly to the browser. This is undoubtedly the fastest and least flexible way to send content. You might wonder at this point when it's appropriate to use print and when you should place text outside PHP tags.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics