PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Variable Manipulation and Output
Pages: 1, 2

Outputting to the browser

One of the most fundamental aspects of web development in PHP is outputting to the browser. Although in a technical sense you are outputting to the browser simply by sending them the content of a web page, when we discuss output we will focus on sending variables that we construct and manipulate in PHP to the browser dynamically.



Basic output with echo
The first function we will discuss, echo, is the most basic output function available to PHP. With echo, you can send any data to the web browser (variable or constant). Its syntax follows:

echo <variable or constant>;

In example:

<html>
<head>
<title>My first PHP page</title>
</head>
<body>

<?php

     $my_msg = "This is my first PHP output!<br />";
     $my_var = "Hello, PHP!<br /><br />";
		
     $msg = $my_msg.$my_var;

     echo $msg;

     $my_var = "Goodbye, PHP!";

     echo $my_var;

?>

</body>
</html>

This example pulls together a number of the concepts that we have been talking about in the past articles. Above, we have a PHP script embedded in a HTML document. In this PHP script, we are taking two variables ($my_msg and $my_var) and assigning them values. These two variables are then combined into a single variable and stored into $msg which is then output to the browser using the echo function. Then the value of $my_var is changed and a second output is made to the browser showing its new value. The PHP script then terminates, returning the server to outputting straight HTML. The resulting output is equivalent to the following static HTML page:

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

<html>
<head>
<title>My first PHP page</title>
</head>
<body>
This is my first PHP output!<br />
Hello, PHP!<br /><br />
Goodbye, PHP!
</body>
</html>

Which of course outputs to the browser as:

    This is my first PHP output!
    Hello, PHP!

    Goodbye, PHP!

Notice that in our PHP code to output the text we included the HTML tag <br /> where we wanted a line break within the PHP variable. Without such HTML tags within our variables, the output would have looked something like this:

This is my first PHP output!Hello, PHP!Goodbye, PHP!

Simplified variable output

Although the echo function is the most basic function used to output to the browser, there is an easier way to output single variables in PHP. The best way to illustrate this is by rewriting our original example:

<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<?php
		
   $my_msg = "This is my first PHP output!<BR>";
   $my_var = "Hello, PHP!<br><br>";
		
?>

<?=$my_msg?><br />
<?=$my_var?><br /><br />

<? $my_var = "Goodbye, PHP!"; ?>

<?=$my_var?>

</body>
</html>

Although the above example looks more complicated than the original, it illustrates an important element of PHP syntax -- the <?= ?> method of output. This example may be different in syntax, but the output to the browser is identical. Using this method is no more efficient or effective than using echo, but exists to save keystrokes for the developer needing to perform the very common task of outputting a variable.

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.


Return to the PHP DevCenter.




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com