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:
- Create a new document in Notepad or similar ASCII
editor called rain.php (note the .php ending).
- 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>
- Once you have typed the above code, upload
your rain.php file to your server.
- 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