Using PHP to Generate Output
The echo() Statement
Without displaying any output, we probably won’t be able to accomplish anything useful with our scripts, hence PHP provides several different methods of passing output to the web browser and essentially providing the user with some feedback. In particular, we’ll cover the echo() statement and the print() statement, which are the two most common ways of displaying output to the browser.
Referring back to our “Hello World” example, you can probably guess what the echo() statement does. It simply displays or echoes output directly to the browser, in this case the string “<p>Hello World!</p>” was passed to the browser and displayed to the user.
<html> <head> <title>Hello World Script</title> </head> <body> <?php //this is some php code echo '<p>Hello World!</p>'; ?> </body> </html>
Just like all PHP statements, you have to end the echo() statement with a semicolon ( ; ). If you accidentally omit the semicolon (a very common mistake), PHP will complain like a madman and throw errors left and right. Lets look at some examples:
<?php echo "<p>Hello World</p>"; echo "<p>Hello"; echo " World!</p>"; //same output as above echo "<br>"; //this will output a line break echo "<p>Hello from PHP, I'm alive!</p>"; ?>
If you tested the examples you should get output similar to the one below. Notice in the last echo statement, the string spans two lines, but in the browser it displays as a single line. White-space characters such as carriage returns, extra spaces, and tabs are ignored by HTML and despite the fact that there is a carriage return in the example above, HTML treats it as a single line.
Hello World!
Hello World!
Hello from PHP, I'm alive!
Print() Statement
The print() statement is very similar in functionality to the echo() statement. It prints the string or expression passed to it directly to the browser, with one main difference. It returns a Boolean value of 1 on successful execution, which can be used as part of a larger expression. The main point to consider is that print() behaves more like a function were as echo() does not.
Some examples follow:
<?php
print("<p>Hello World!</p>");
print "<p>Hello World!</p>"; //same output as above
$lunch = "cheeseburger"; //declaring a variable
print("$lunch"); //print() can also accept variables
?>
Looking at the output, we can be safe to say that both statements work in an identical manner. Both the echo and print statements can accept a variable or an expression as a valid argument and can function without the parentheses. So what’s the advantage of using one statement over the other? Since echo does not return a value, it is said to be a little faster then the print function, but you can choose either one to your heart’s desire.
Hello World!
Hello World!
cheeseburger
There are several other output functions available, more precisely the printf() and sprintf() functions, which allow you to apply formatting to the arguments before displaying them. We’ll cover these functions plus some other useful functions in later tutorials. On the next page, we introduce a very important concept in programming; variables and how to implement them to create interactive scripts.
