Sending Text to the Browser

I l @ ve RuBoard

PHP would not be very useful if all you could do was see what PHP features were installed. What you will use it to do most frequently is to send information to the browser in the form of plain text and HTML tags. For this, you will use the print() function.

To print a simple message:

  1. Open first.php in your text editor.

  2. Put your cursor between the PHP brackets and create a new line by pressing Return.

  3. Type print ("Hello, world!"); (Script 1.3).

    Script 1.3. By putting the print statement between the PHP brackets, we have the server send the Hello, world! greeting to the browser. It's the same as if we had put Hello, world! within the HTML code.

    graphics/01sc03.gif

    Printing the phrase Hello, world! is the first step most programming references teach you. Even though it's a trivial reason to use PHP, I'll abide by the convention for demonstration purposes.

  4. Save your script.

  5. Upload your script to your server and test it in your browser (Figure 1.3).

    Figure 1.3. If your script has been executed correctly, your browser should look like this (not very exciting, but it works).

    graphics/01fig03.gif

Tip

There are different functions you can use to send text to the browser including echo() and printf(). Echo is virtually synonymous to print, so it will not be discussed in any more detail. I will cover printf() in Chapter 13, Creating Web Applications.


Tip

You can use print() with or without the parentheses but you must always use the quotation marks, e.g., you could have typed print "Hello, world!"; While we'll use parentheses in this book to further separate out printed statements, most programmers do not use them. My suggestion is to find a style that you like and stick to it.


Tip

Failure to use an opening or closing quotation mark or parenthesis or neglecting to include the required trailing semicolon for every statement are common causes of errors when using the print() statement. If your script is not executing properly, check these items first.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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