More Printing Power


There's a difference between displaying text at the command line and displaying text in a browser. In a browser, you use HTML elements such as <BR> and <P> to format your text. When you print out text at the command line, you can use special characters for formatting if you enclose that text in double quotes. Here are those special characters:

\n

Newline character

\r

Carriage return

\t

Tab

\\

Displays a \

\$

Displays a $

\"

Displays a "

\0 to \777

Displays a character corresponding to a hexadecimal (base 8) code

\x0 to \xFF

Displays a character corresponding to a hexadecimal (base 16) code


For example, echo "Line 1\nLine 2" will display "Line 1" on one line and "Line 2" on the nextif you're running PHP at the command line. In a browser, the \n newline character means nothing. For the same result, you should use echo "Line 1<BR>Line 2".

NOTE

This is an important item to rememberif you're displaying text in a browser, you must format that text using HTML tags. Just sending line breaks in your text won't do anything because the browser will take those line breaks out automatically, just as it would in any web page. To format your text as you want it, you must use your PHP scripts to send proper HTML to the browser.


If you want, you can break up a long quoted string across various lines in your script, and the line breaks will be preservedif you're printing at the command line. If you print to a web page, the line breaks will be ignored:

 <?php echo "This text spans multiple lines."; ?> 

You can also separate the items you want printed with commas, like this:

 echo "Hello", "this", "is", "PHP."; 

All the items you want printed this way are printed, one right after another:

 HellothisisPHP. 

If you want to include spaces between the words, do something like this:

 echo "Hello ", "this ", "is ", "PHP."; 

This would give you:

 Hello this is PHP. 

If you want to print a sensitive character such as a " without telling PHP that you're ending your text (which a " mark would otherwise do), you can use \" instead this way:

 echo "He said, \"I like ice cream.\""; 

This is called escaping the quotation mark so that PHP will display it instead of treating it as marking the end of a text string.

In PHP, you can also assemble text strings together into one string using a dot (.). Here's an example:

 echo "Hello " . "this " . "is " . "PHP."; 

In this case, PHP takes your expression "Hello " . "this " . "is " . "PHP." and assembles it together (this is called concatenation) into one single string, "Hello this is PHP.", and then passes that string on to the echo statement.

Here are a few echo examples:

echo 11115555;

Displays: 11115555

echo "Hello from PHP.";

Displays: Hello from PHP.

echo 'Hello from PHP.';

Displays: Hello from PHP.

echo "Hello", "from", "PHP.";

Displays: HellofromPHP.

echo "Hello " . "from " . "PHP.";

Displays: Hello from PHP.


Besides echo, you can also use the PHP print statement using the same syntax, like this: print "Hello from PHP.";. What's the difference between print and echo? Not much, really; print is more like a PHP function (see Chapter 4, "Breaking It Up: Functions"), so it r eturns a value, which is always set to 1. As with other functions, you can read this value, but in this case you can't do much with it. For most practical purposes, echo and print work the same way in PHP, so which one you use is up to you.



    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