Introduction to PHP
       by kirupa : 21 June 2004

PHP is a great language that web developers such as yourself should know. PHP, which stands for PHP: Hypertext Preprocessor, is very useful for adding simple interactivity or communicating with databases. Best of all, PHP is not very complicated to learn.

Unlike other languages, PHP does not require rigorous training and a long time of practice before you can create anything useful or publically displayable. In fact, by the end of this tutorial, you will have created a web page that displays some text from a PHP script!

What Do I Need to use PHP?
To display PHP, you will need to have a web server that is properly configured to display PHP. Most web hosts such as mediatemple (which hosts this site) have PHP enabled by default, so you should be set to go. To test if your server is capable of running PHP scripts, try the method outlined in the following tutorial: http://www.kirupa.com/web/testphp.htm

If you do not have PHP installed, you should contact your web host and request them to enable or install PHP for you. If you are planning on setting up your own web server, there are a lot of great resources on the net that will explain how to do that.

To create PHP code, all you need is any program that allows you to enter code without adding extraneous formatting. You can use any program such as Notepad or even the code-views of FrontPage and Dreamweaver. For this tutorial, I will be using Notepad for simplicity reasons. Once you become familiarized with the PHP language, feel free to use any program that allows you to write PHP scripts.

So, let's get started with a very basic PHP example that you will create!

Here is how:

  1. Create a new document in Notepad or similar ASCII editor called rain.php (note the .php ending).
  2. Now that you have created the file called rain.php, enter the following code in your Notepad window:
<html>
<body>
<?php
print("The developer in Spain codes gently in the rain.");
?>
</body>
</html>
  1. Once you have typed the above code, upload your rain.php file to your server.
  2. After you have uploaded the file, open that file in your browser. If PHP is configured properly, you should see the sentence The developer in Spain codes gently in the rain. displayed in your browser.

    For a live example, click on this link.

Congratulations! You have created a working PHP example. Now that we have crossed that milestone, let's learn why the above code worked.

Code Example
The following section explains why the code works the way it does.

<?php
print("The developer in Spain codes gently in the rain.");
?>

While PHP works with HTML, you need to find a way to differentiate PHP code from the HTML code. You cannot place PHP code randomly inside another HTML document. The solution is to place all PHP tags between <?php and ?> tags as shown above.

<?php
print("The developer in Spain codes gently in the rain.");
?>

You use the print command to display something in your browser. In our example, all you are doing is simply displaying the sentence to our browser.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.