PHP Exercise 1
Basic PHP syntax
- The first thing you need to understand is
basic HTML and how PHP works with HTML. The first useful thing to do is
create a very simple PHP page that simply prints out some HTML code to
give you a web page.
- Throughout this tutorial, you can either use
the terminal based vi program or you can open up a
KDE text editor similar to Windows notepad by clicking on the red hat in
the lower left hand corner of your screen and going to Accessories > More
Accessories > Text Editor
- Create a file called new_php_file.php in the
/var/www/html directory and open a text editor in a terminal window by
doing the following:
shell > cd /var/www/html
shell > vi new_php_file.php
- Then hit i (if you created the file in vi) on the keyboard and type the following:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo "<p>Hello
World<p>"; ?>
</body>
</html>
- When you are done entering this text, hit the ESC key on the keyboard followed by :wq which means write
and quit (only if you use vi ). To view your new file, open your web browser and type
in http://localhost/new_php_file.php in the
address bar. You could accomplish the same thing by making a file
called new_html_file.html and insert
the following lines:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p>Hello World<p>
</body>
</html>
- So after comparing the two versions we see
that the php section started with <?php
and ended with ?>. The echo""; function in php printed out HTML code to the browser.
- Figure 6.1 shows a screen shot of the output
from the new_php_file.php

Figure 6.1
- If you created both of these files, you should
seem the same thing on the screen when viewing both files. If your .php
file doesn't work correctly, make sure you followed the Apache
installation proceedures correctly.
- Next let's look at another built in PHP
function called phpinfo(). This function will tell us everything we need to know about our
installed PHP interpreter. Go back to your terminal window and type
shell > vi new_php_file.php
10.After you open the file, replace the following
line:
<?php echo "<p>Hello
World<p>"; ?>
with the following:
<?php
echo "<p>Hello World</p><br>";
phpinfo();
?>
11.
Be sure to hit ESC and then
:w to save the file.
12.
This will print out
to the screen a huge amount of information regarding the PHP interpreter, your
Apache server, and other components of your machine.
13.
Note that PHP doesn't
particularly care about "white space", new lines, tabs, etc. Certain functions in PHP do have "white space" requirements.
On Your Own Exercise 1
- Create an .html file that displays information
about yourself, including name, age, and favorite color. Then copy and
paste the HTML into a .php file and use the echo() function to display
your HTML.