PHP is not simply about outputting random values and
simple text. You can seamlessly integrate PHP with HTML and
output tables, formatted text, and more. You are probably
wondering, what's the big deal about this? The deal is that
you can control what is displayed on your site by
integrating PHP and HTML. Best of all, PHP does not have its
own set of formatting tags for you to learn. You use HTML.
So let's get started. First, copy and paste the following
code into a PHP page:
- <html>
- <body>
- <?php
- print("Can
gophers really code PHP?");
- ?>
- </body>
- </html>
When you preview the page containing the above code, you
should see the text "Can gophers really code PHP?" The text
is not formatted at all. You will see the text displayed in
whatever the default font is set to on your browser.
You don't have to suffer through dull, boring text
though. You can format it - all inside the PHP tag area.
Let's say you want to Bold the text. Add the HTML tags for
bold, <b> and </b> tags to your text:
- <?php
- print("<b>Can
gophers really code PHP?</b>");
- ?>
When you modify the code within your PHP tags with your
Bold tags (see above code) and preview the page in your
browser, you will find that the text is now bolded. Pretty
cool, ehh? Now, let's try adding a horizontal line below the
text. The html tag for displaying a horizontal line is
<hr>.
Let's add that tag to our code:
- <?php
- print("<b>Can
gophers really code PHP?</b>");
- print("<hr>");
- ?>
Now, test this code. You should now see your text with a
horizontal line displayed. Just remember, all we used was
standard HTML tags using the print command.
Outputting Data Involving Quotes
While all of this is pretty straightforward, there will be
situations where you simply cannot place a series of HTML
tags within the confines of the print command. The following
is one such example
Let's say we want to make our text a hyperlink. The
HTML for doing such a trick is:
Now, we cannot simply place the above HTML inside the
print command. The reason is because of the dreaded
quotation marks surrounding the URL. PHP misinterprets the
quotation marks as referring to the end of the print
command. You will probably end up with an error.
The solution for outputting quotation marks, is to use
the following two characters (quotation mark and back slash)
in the place of a quotation mark:
\" .
Therefore, the print command for the above HTML code
becomes: