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

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.




Recommended for You

Tagged Articles

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

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • Partner Sites
  • makezine.com
  • makerfaire.com
  • craftzine.com
  • igniteshow.com
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com