How About Printing Out Some Text?


The echo statement, which inserts text into a web page, is just about the most versatile and common statement in PHP. We're going to be using this statement a lot to display the results of doing something with PHP, so let's get started now.

You use the echo statement simply with the keyword echo and by giving it some quoted text to display. You can see how the echo statement might display the text "Hello from PHP." in this script:

 <HTML>     <HEAD>         <TITLE>             Using the echo statement         </TITLE>     </HEAD>     <BODY>         <H1>             Echoing some text:         </H1>         <?php             echo "Hello from PHP.";         ?>         .         .         . 

Because you can intersperse PHP scripts throughout an HTML page, you can echo text at multiple locations in a web page using PHP, as you see in Example 1-2, echo.php.

Example 1-2. Using the echo statement
 <HTML>     <HEAD>         <TITLE>             Using the echo statement         </TITLE>     </HEAD>     <BODY>         <H1>             Echoing some text:         </H1>         <?php             echo "Hello from PHP.";         ?>         <H1>             Echoing some more text:         </H1>         <?php             echo "Hello from PHP again!";         ?>         <IMG src="/books/1/265/1/html/2/php-power-white.gif">     </BODY> </HTML> 

The results appear in Figure 1-6.

Figure 1-6. Using echo.


You can enclose the text you want to echo in single quotes or double quotes, and you can also echo numbers without having to quote them at all, like this:

 echo 111555; 

Because what you're echoing goes to a web page (unless you're executing PHP on the command line), it's also useful to echo HTML tags back to the browser. For example, if you want each word to appear on a different line, you can use HTML <BR> elements like this:

 echo "Hello<BR>from<BR>PHP."; 



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net