Visit the Official PHP Website
Search this Site

PHP Programming - Lesson 1c

By James N Hitz

Ambitious Alternative

You must be feeling ambitious by now. I know I am. So let's make our program do a little more than just "Hello World"-ing the PHP world. Let's say "Hello PHP World", output a "<HR>" and a line saying "PHP seems like fun" in red and a size of 4. In "plain HTML" this would be printed thus:

<h2>Hello PHP World
<hr>
<p><font size="4" color="red">PHP seems like fun</font>

In PHP there is a right solution and a wrong solution. Here is the wrong solution:

<html><head><title>Hello PHP World</title></head>
<body>
<?php
     print("<h2>Hello PHP World");
     echo("<hr>");
     print("<p><font size="3" color="red">PHP seems like fun</font></p>");
?>
</body></html>

What! This is wrong!? Perl and Java(Script) programmers must be smiling by now. The other like you (oops! Sorry) are just buffled. Let's take a refresher.

In point number e) of the points (observations) made earlier we said the text being output should be put WITHIN double quotes. Look at line 6 in our code. How many double quotes do you see? Well. The PHP preprocessor would be as confused as you if you gave it the above code to munch :-O.

How about that echo thingie on line 5. Well. That's fine. There are more than one ways of killing a rat: dip it in hot water, chock it with a blanket, smash it with a slipper, shoot with a canon...

In the same way, there are several ways of outputing text, print() just happens to be one of them and echo() is one of the other sisters.

All right. So the echo() is not a blunder but the quotes in line 6 are. Now What!?

<< Hello PHP World | Now What? >>