How to Write a Web Page Using PHP

SethCoder
PHP is a very good scripting language used for server side scripting. You can use it to do a lot of neat things that you can't do with HTML by itself. To use PHP to create a web page you must have access to a web server that allows PHP scripts. You can download XAMPP from Apache Friends and it will install Apache web server, PHP, MySQL and configure it automatically. Best of all it's free. Alternatively you can purchase a web hosting service from a commercial provider.

Once you get the web server set up you can begin writing your scripts. If you are using XAMPP you can just edit the files right on your hard drive and then browse to the location. If you have a remote web server you must upload the files after you edit them with an FTP client.

Here is a short PHP script that will display a webpage.

echo "Hi this is my webpage";
?>

You may be wondering why you would even use PHP to do this. The example above only displays HTML code, but using variables and extra functions you can create something a little more fun. You can create sessions to allow for variables to be saved when users change pages on your site, and using this you can have users log into your site. Anytime you have to log into a website, sessions are used. Sessions are not restricted to PHP, but PHP has support for them.

Try the following, it adds two variables into another variable then displays the result.

@session_name('scoder');

@session_cache_expire(99999);

@session_start();
$c=$_SESSION["c"];
$a=4; $b=1;
$c=$c+$a+$b;
echo "$c";
$_SESSION["c"]=$c;?>

The result will be 5. If you refresh the page it will add 5. So if you refreshed it 5 times, it would display 25.

Now try this.

echo getenv("REMOTE_ADDR");
?>

You will see your own IP Address, which can be very handy. This is what gets logged on the web servers whenever you visit a site.

Once you get familiar with the basics of PHP things will become more easy. You can connect to databases and make your web site come alive using the built in PHP MySQL functions.

This is only a very short example of what can be done with PHP. There is so much more you can do with the language that can not be summarized. I recommend you visit www.php.net for more information if you are interested in learning more.

Published by SethCoder

I work with a lot of different technologies and have certifications on a number of various electronics systems. I am in the United States Air Force, and my job is known as Cyber Transport Systems Specialist...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.