Our First PHP Script
Introduction.
I think that Hello World! programs started with Kernigan and Ritchie's
introductions to C many years ago. But regardless of the actual
history, our first script will essentially be another Hello World!
program. In this setting we will also discuss commenting our scripts
and making sure they send information to the browser. You should save this first file called hello_world.php. |
<html> <head> <title>Test PHP Script</title> </head> <body bgcolor = "000066"> <?PHP // using the print function to convert things to HTML print("<center><font size = '5' color = 'cccccc'><b>Hello World!</b></font></center>"); ?> </body> </html> |
At present I have distinguished the embedded PHP code
by changing its color to purple. The comment line is represented in
green. Notice that most of the file is standard and simple HTML. You should also notice that all the lines of code are terminated with semicolons. This will be true for all but a very few types of code. Comments. Lines of PHP can be commented in two main ways, similar to Java and JavaScript. Double slashes
can be used so that any characters after them on a line will be considered to be comments. These do not necessarily need to appear at the beginning of a line.
While there are some other approaches, these are the two I will make use of. Sending Info to the Browser. At present we will work with two main ways to send information to the browser.
There is not a lot of difference between the two. All of the following
should work. But you should notice how HTML within the PHP must be sent within the quotation marks within the print( ) or echo( ) functions. HTML does not work directly within PHP. This will cause us to jump back and forth between blocks of PHP and HTML quite often. Escape Characters. But what if you want to display quotation marks of some sort or another? In these sorts of instances you must place a backslash
immediately before the delimiting character you want to have actually display. This can be seen in the following script which you should call escape_characters.php. |
<html> <head> <title>Using Escape CHaracters</title> </head> <body bgcolor = "000066" text="cccccc"> <?PHP /* making use of echo and print functions making use of " and ' within the text to be displayed */ print("<center><font size = '5'><b>You say, \"hello world\"!</b></font></center><BR>"); // comment at end echo("<BR><center><font size = '5'><b>I\'ll say, \"goodbye world\"!</b></font></center>"); ?> </body> </html> |
Some other major escape characters are presented in the following table. |
Characters | Description |
\" | allow " to be printed/echoed |
\' | allow ' to be printed/echoed |
\\ | allow \ to be printed/echoed |
\$ | allow $ to be printed/echoed |
\n | new line |
\r | carriage return |
\t | tab |