Thursday
PHP & MySQLMixing PHP in Some HTML
Essential PHP
- Creating Your First PHP Script
- Running PHP on the Command Line
- Mixing PHP in Some HTML
- Creating PHP Variables

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 place your PHP statements inside a script restricted 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 showed 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 makes 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 grow in the browser’s title bar) and uses an <H1> HTML header to show 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 productivity 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 makes the HTML for a table land 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, everywhere we’ve downloaded php-power-white.gif and are showing 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 vacant to be using this statement a lot to show the results of doing something with PHP, so let’s get ongoing now.
You use the echo statement simply with the keyword echo and by charitable it some quoted text to show. You can see how the echo statement might show 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 right through 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 grow 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 (except you’re executing PHP on the command line), it’s also helpful to echo HTML tags back to the browser. For example, if you want each word to grow on a different line, you can use HTML <BR> fundamentals like this:
echo "Hello<BR>from<BR>PHP.";
More Printing Power
Here’s a difference between showing text at the command line and showing text in a browser. In a browser, you use HTML fundamentals such as <BR> and <P> to format your text. When you print out text at the command line, you can use special font for formatting if you enclose that text in double quotes. Here are those special font:
\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 show “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 must use echo “Line 1<BR>Line 2“.
This is an vital item to rememberif you’re showing 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 involuntarily, 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 show it instead of treating it as marking the end of a text string.
In PHP, you can also assemble text strings collectively 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 collectively (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.
Above and further than 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. Showing a here document.
Here’s another printing option: you can also start “here” documents, which cause echo or print to show everything it reads until it encounters an end nominal 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>
Showing here Documents
</TITLE>
</HEAD>
<BODY>
<H1>
Showing here Documents
</H1>
<?php
echo <<<END
This example uses
"here document" syntax to show all
the text until the ending nominal is reached.
END;
?>
<BR>
<BR>
<IMG SRC="php-power-white.gif">
</BODY>
</HTML>
The result of this here document appears in Figure 1-7.
Post Tags: Apache, Essential PHP, HTML, HTML Tag, Install PHP Locally, Installing PHP, Komodo, Learn HTML Basic, Learn PHP, Maguma, Personal Homepage, Personal Web Server, PHP, PHP FAQ, PHP for Beginner, PHP for Newbie, PHP Logo, PHPEdit, PWS, Running PHP, Running PHP Script, Zend Studio
Related Posts
- Tonium Pacemaker Portable DJ MP3 Player and the DJ Mix Portal
- RT: @Microsyntax Sets Out To Make Sense Of #twittergrammar
- Creating PHP Variables
- Building an Image Gallery in a 3D HTML Table
- Adobe CS5
- Styling Elements of Nested HTML Lists
- Designing with Nested HTML Lists
- Returning Strings from Views with Code Igniter
- HTML for a Common Browser Menu
- An Overview Of Coding With PHP
Popular
- iPhone or iPod Touch, Which One Should You Choose? - 47,761 views
- Introduction to Facebook - 26,655 views
- 7 Top Twitter Topic Trackers - 16,375 views
- Introduction of Hacking Methodology - 15,051 views
- Google Hacking Keywords - 11,716 views
- LG BD390 Wi-Fi Blu-ray Player Review: So Packed You’ll Forget About Blu [Review] - 10,094 views
- Complete Guide to Web Site Marketing - 9,774 views
- BlackBerry Curve 3G: A Familiar, Powerful Phone - 9,774 views
- Microsoft’s Zune HD Wiill Have OLED, HD Radio - 9,300 views
- Photo Album and Printing Services - 8,953 views
Featured Articles
- The Elements of the Facebook Platform
- Introducing the Facebook Platform
- Social Skills Today Are Being Lost
- GibBook – Gibraltar Own Social Network Site
- 7 Deadly Twitter Sins
- Introduction to Facebook
- Google’s Wave: Many Online Apps in One Tool
- App Mapping War Casualties Debuts for Memorial Day Weekend
- Google@Omgili Mashes Traditional Web Search With Social Buzz
- Capital Factory: Austin-Based Incubator (RWS Interview)
Recent Posts
- Tweet via something
- Lightroom Review And Other Phot
- Several Words About The Advantages Of Tablet PCs
- Dissimilar Positive Features Of Table Computers
- Digi Link Doctor
- Why You Need Professional SEO Software
- Automated SEO Tools For Online Business
- How To Choose The Best Tablet PC
- Tablet PC – The Latest Computer Technology
- Tablet PCs Compared To Laptops