Sie sind auf Seite 1von 2

Mixing In Some HTML

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 bound
ed 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 te
xt in the web page as it's
sending the web page back to the browser. In the same way, if a PHP script creat
es some text to be inserted in
the web page sent back to the browser, that text is inserted into the web page a
t 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>
After the <H1> header HTML element, the server will encounter our PHP script, wh
ich means that the output of
the phpinfo function will be inserted into the web page we're sending back to th
e browser exactly at that point.
As you know, the phpinfo function creates the HTML for a table holding informati
on about the PHP installation,
so the result is what you see in Figure 1-4the HTML header appears first, follow
ed by the PHP information table.
Figure 1-4. Mixing PHP and HTML.
[View full size image]
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 w
eb 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();
?>
<<IIMMGG SSRRCC==""pphhpp--ppoowweerr--wwhhiittee..ggiiff"">>
</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.
[View full size image]
How About Printing Out Some Text?
The echo statement, which inserts text into a web page, is just about the most v
ersatile and common statement
in PHP. We're going to be using this statement a lot to display the results of d
oing something with PHP, so let's
get started now.
You use the echo statement simply with the keyword echo and by giving it some qu
oted text to display. You can
see how the echo statement might display the text "Hello from PHP." in this scri
pt:
<HTML>
<HEAD>
<TITLE>
Using the echo statement
</TITLE>
</HEAD>
<BODY>
<<HH11>>
EEcchhooiinngg ssoommee tteexxtt::
<<//HH11>>
<<??pphhpp
eecchhoo ""HHeelllloo ffrroomm PPHHPP.."";;
??>>
.
.
.
Because you can intersperse PHP scripts throughout an HTML page, you can echo te
xt at multiple locations in a
web page using PHP, as you see in Example 1-2, echo.php.

Das könnte Ihnen auch gefallen