PHP stands for PHP: Hypertext Preprocessor (yes, it is a recursive acronym, in that PHP is one of the words of the acronym), and is a scripting language that can usually be found running on Apache servers. It is an Open Source language (for more information about Open Source, visit OpenSource.org). It is very powerful, and very easy to learn, which along with the fact it runs on Apache (Open Source) and is itself Open Source makes it one of the most popular scripting languages in use on the web today.
There are several versions of PHP, the current one being PHP 4, though at the time of writing PHP 5 is due for imminent release.
PHP pages usually end with the extension ".php", ".phtml" or ".php3", for example "index.php". Pages that have a recognised extension, on a properly configured server, will be processed by the PHP scripting engine before being returned to the user. Anything within the tags '<?php' and '?>' is code and will be processed by the engine.
Which brings us neatly to our first piece of code. The first thing you will learn to do with PHP is use it to write a simple piece of text to a web page (the famous "Hello World").
<html>
<body>
<?php
echo 'Hello World!';
?>
</body>
</html>
Copy and paste the above to an empty text file (for example in Notepad). Save it as "php101.php", and upload it to a server that supports PHP. When you load the page in a browser, you should see the text "Hello World!". If you do, congratulations! You've finished your first script. If not, it may be that your server does not support PHP - the above should work on any server with PHP support.
What you have done in the above example is write out a normal HTML page, but you have used the PHP scripting engine to write out the text within the page. A good start, but not the most useful of tools so far. Next, we'll learn how to use basic variables.
Variables in PHP are very easy to use. You can spot a variable, because its name will almost always begin with a "$" sign. It is always a good idea to name your variables so that you can see easily what they refer to. For example, a variable containing a first name might be called "$FirstName".
To use these in a script is reasonably simple:
<html>
<body>
<?php
$HelloWorld = 'Hello World, Again!';
echo $HelloWorld;
?>
</body>
</html>
Note that when you use the variable with the "echo" section, you should not put quotation marks around it. If you do, the script will just write out "$HelloWorld" as text, rather than writing the value you gave it.
The above is a simple process - a variable is created and assigned the value "Hello World, Again!" and written out to the web page. If you copy and paste the above as before, and upload the new script and open it in a browser, you should now see the text "Hello World, Again!" on the page. If you do, congratulations, you have just finished your second PHP script.
Still, though, not the most impressive piece of interaction on the web. Next though, we're going to write a more complex script. This time, we're going to create a variable, give it a value of the current hour of the day, and display a piece of text based upon whether it is morning or afternoon.
<html>
<body>
<?php
$HourOfDay = date("G");
if ($HourOfDay < 12) {
echo 'Good Morning World!<br><br>The hour of the day is ' . $HourOfDay;
} else {
echo 'Good Afternoon World!<br><br>The hour of the day is ' . $HourOfDay;
}
?>
</body>
</html>
First, we grab the current time using the "date()" function. The argument, "G" (an argument is a value you pass a function) tells the date function to give us the hour of the day, with leading zeroes. At this stage, $HourOfDay will contain a number representing the hour of the current day.
Next, we check the value. If the value is less than 12, we know the current time is morning, and write out an appropriate greeting, followed by the hour of the day (you use a period to write more than one thing to a page at once). Otherwise (else) it is afternoon, and we write out a different greeting.
Now, to use this, you simply need to upload this script, as before, to your web server, and open it in a web browser. Your server may be on a different time zone to you, but you can see what hour of the day it thinks it is underneath the greeting. What you should now see is a greeting of either "Good Morning World!" or "Good Afternoon World!" followed by a blank line, then the hour of the day.
And voila - your first piece of interactive web programming. Congratulations, you've taken your first steps in PHP!
13 Comments
this does not help localhost developers !
#1, Bart, Belgium, 5 March 2005. Reply to this.
it's an intro to php page, not an intro to local host developing. I find this page extremely useful and am very grateful to the author for publishing it. If not for pages like this we would have to have volumes upon volumes of how to books.
Replies: #11.
#2, Ben, United States, 30 June 2005. Reply to this.
thanks for the php tutorial
#3, Jason, United States, 30 June 2005. Reply to this.
Thanks for the php introduction. How about some links to further examples?
#4, Nigel, United Kingdom, 14 August 2005. Reply to this.
Hey thanks for the article! I've been half-heartedly trying to convert from ASP.NET to PHP recently, but have been hitting a lot of walls with little things. You should go into more basics, as your style is easy to read as well as direct. Thanks!
#5, Chris, United States, 15 August 2005. Reply to this.
Very clear, concise and a great introduction. Grab yourself a WAMP stack like InstantPHP that includes a ready to run combo of Apache, MySQL and PHP, and join the revolution!
#6, Arka, United States, 26 March 2006. Reply to this.
Thanks for the intro.
Desperately trying to figure this out as a "right-brainer!"
Love your PHP & CSS cheat sheets as well!
I would also like links to more info!
Is there a PHP script writer with a good GUI (for the Mac) out there?!
#7, Jerome, United States, 11 April 2006. Reply to this.
no thx, pls do not write this type of loose article.... next time no one can read this this is totaly west
#8, manish, Australia, 10 February 2007. Reply to this.
whats with these foreigners? dust off the haters.
gr8 intro pg. really love yr cheat sheets.
#9, anonymouse, United States, 15 August 2007. Reply to this.
please help....
i'm newbie in php. right now i'm using php 5.2.6 mysql 5.0 and apache 2.0 on win xp sp2.
i got this error message : Fatal error: Call to undefined function mysql_create_db() in C:\htdocs\bikindb.php on line 19
when trying to execute this code :
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "marais";
$db = "testdb";
// open connection
$conn = mysql_connect($host, $user, $pass) or die ("Unable to connect Database!");
// create database
mysql_create_db($db) or die ("Unable to create database!");
// create query
$query = "CREATE TABLE alamat (
nama VARCHAR (25) NOT NULL,
alamat VARCHAR (60) NOT NULL,
telpon INT (11) NOT NULL,
id INT (3) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)";
$hasil = mysql_db_query($db,$query);
if ($hasil) {
echo "Tabel <b><i>alamat</i></b> dalam <b>$db</b> berhasil dibuat!";
}else{
echo "Tabel <b><i>alamat</i></b> dalam <b>$db</b> gagal dibuat!";
}
?>
</body>
</html>
did i made mistake on the syntax?
please tell me at : as_syaroni@yahoo.com
#10, marais, Unknown, 5 November 2008. Reply to this.
#2 - All hail the publisher! Down with dissent!
#11, Alpha, Australia, 17 January 2010. Reply to this.
@marais.
this is not really a support forum.
if you need help, try stackoverflow - it's dedicated to those kind of questions.
#12, murraybiscuit, en-ZA, 11 May 2010. Reply to this.
Good Tutorial.
#13, Debashish Deka, India, 17 February 2012. Reply to this.