PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


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.




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: