
PHP files are most often mixtures of HTML and PHP scripts. Putting HTML and PHP scripts into the same document is no problem because you put your PHP statements inside a script bounded by <?php and ?>, which means the server can pick them out easily.
You already know how the HTML in web pages work; when you insert some HTML that displays text, for example, that text is displayed when the web server reaches the line with the text in the web page as it’s sending the web page back to the browser. In the same way, if a PHP script creates some text to be inserted in the web page sent back to the browser, that text is inserted into the web page at the location of that script.
For instance, take a look at Example 1-1, phphtml.php. This file contains a mix of HTML and PHP. As you can see, the HTML sets the title of the document (which will appear in the browser’s title bar) and uses an <H1> HTML header to display the text Mixing HTML and PHP! in large bold font.
Example 1-1. Mixing HTML and PHP
<HTML>
<HEAD>
<TITLE>
Mixing HTML and PHP!
</TITLE>
</HEAD>
<BODY>
<H1>
Mixing HTML and PHP!
</H1>
<?php
phpinfo();
?>
</BODY>
</HTML>

Figure 1-4. Mixing PHP and HTML.
After the <H1> header HTML element, the server will encounter our PHP script, which means that the output of the phpinfo function will be inserted into the web page we’re sending back to the browser exactly at that point. As you know, the phpinfo function creates the HTML for a table holding information about the PHP installation, so the result is what you see in Figure 1-4the HTML header appears first, followed by the PHP information table.
Want to make your PHP page look more official? You can find some “Powered by PHP” logos at http://www.php.net/download-logos.php. Just download them and add them to your web page using an <IMG> element like this:
<HTML>
<HEAD>
<TITLE>
Mixing HTML and PHP!
</TITLE>
</HEAD>
<BODY>
<H1>
Mixing HTML and PHP!
</H1>
<?php
phpinfo();
?>
<IMG SRC="php-power-white.gif">
</BODY>
</HTML>
You can see an example in Figure 1-5, where we’ve downloaded php-power-white.gif and are displaying it in a PHP-enabled web page.

Figure 1-5. Adding a PHP logo.
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="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.";
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 "
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“.
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, 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.
Printing “Here” Documents

Figure 1-7. Displaying a here document.
Here’s another printing option: you can also create “here” documents, which cause echo or print to display everything it reads until it encounters an end token you specify (usually END), which must be placed at the very beginning of a line. You can see what this looks like in Example 1-3, phphere.php.
Example 1-3. Printing a here document
<HTML>
<HEAD>
<TITLE>
Displaying here Documents
</TITLE>
</HEAD>
<BODY>
<H1>
Displaying here Documents
</H1>
<?php
echo <<<END
This example uses
"here document" syntax to display all
the text until the ending token is reached.
END;
?>
<BR>
<BR>
<IMG SRC="php-power-white.gif">
</BODY>
</HTML>
The result of this here document appears in Figure 1-7.

