Login
Password

Forgot your password?

How to write your first PHP webapplication

By Edited Apr 3, 2016 1 0

PHP is a programming language that allows you to generate webpages on the fly. PHP has been developed originally by Rasmus Lerdorf in 1994 and started out to replace a set of perl scripts to do recuring things on Rasmus homepage. He called this the Personal HomePage toolkit (hence PHP). I was developed C and thus has a C-style syntax. People who are used to programming in any C style language will find the syntax very recognisable. After a while Rasmus added functionality to connect to a database and published his code as opensource on the internet. As with many opensource projects, the usage and development exploded, and it is now one of the most used scripting language for website, competing with Microsoft's ASP.

This guide will show you how to create your first PHP application.

Things You Will Need

  • You will need a webserver that supports PHP. This can be an account with a webhosting provider or a copy of the apache/php server software on your local computer like xampp
  • This guide assumes you have a basic knowledge of standard HTML
  • You will need a text editor, or even better, a PHP editor like Zend Studio or Eclipse.
Please note that infobarrel cannot display < tags > properly without spaces between the < > and the tagname. All tags in this article are thus written with a space. If you copy the code, you will have to remove all the spaces between the < > and the tagnames before it will work.

Step 1

Lets make a basic "Hello World" HTML page first, just to refresh your HTML. Lets make standard white page with "Hello world" centered on it.

< html >
< head > < title > Hello World! < /title >< /head >
< body >
< div align="center" >Hello World !
< /body >
< /html >

That should have been easy enough, if you are having problems with this, I advise you to read up about basic HTML first.

Step 2

Now, before we start doing PHP, lets explain some things first:
All PHP code should be included within the php parser tags: < ?php [code] ? >
You can also put in larger chunks of code like this:
< ?php
[larger
chunk
of code]
? >

This can be mixed in a HTML page: < div align="center" >< ?php [code] ? >< /div >


Step 3

So lets put something dynamic on our hello world webpage. Like for example, the current date and time.
For this we can use the date function, which will return a string with the date and time formatted as specified in a string.

The format string can be something like: "Y-m-d H:i:s" which would return the current date and time in the format of: 2010-01-27 11:36:54. You can shift around the letters to get another format, for example: "d/m/Y" will return 27/01/2010. For all the format options, check out the PHP.net page about the date function.

So lets include the date in our HTML page, generated by PHP code:

< html >
< head >< title >Hello World!< /title >< /head >
< body >
< div align="center" >Hello World! Today the date is < ?php echo date("m-d-Y"); ? >< /div >
< /body >
< /html >

This shall display as:
Hello world, Today the date is 01-27-2010

As you can see, I have also used the echo function. This function will display a string to the user. Since the date function return a string, echo will take the string that the date function returns as input and will display it to the user. You could also use < ?= as a shorthand for "< ?php echo". In the example above we could have also written: < ?= date("m-d-Y"); ?>

Also note that there is a ';' at the end of the line of code. As with all C-style syntax languages, this is a requirement. It tells the parser: This is the end of this line of code. There are exceptions like for example a function or class declaration, or in a if/else or while statement.
If you do not include the ';' at the end of the line, PHP will tell you about it in an error message.
For example, if we would feed this piece of code to PHP:

echo "Hello"
echo "World!";

The parser would then complain that it has got an 'echo' while it expected a ';'. This is because without the ';' after the first line, the parser starts reading the second line right after it.

Lets get the date format in a better english:

< div align="center">Hello world! Today is < ?= date("F, ds Y") ? >< /div>

This would display:
Hello world! Today is January, 27th 2010


Step 4

So you just wrote your first PHP application. How does it feel ?
Want more ? Lets do a simple If statement.

Lets say that instead of Hello World we want to say Goodmorning World, Good afternoon World, Goodevening World and Goodnight World, depending on the server time.

We can use simple if statements for this.
The syntax for an if statement is:

if (condition) {
[code to execute when condition is true]
}

Condition has to return a boolean true or false. This can be an equasion, or some other formula that would return true or false.
For now it is important that an equasion in a C-style language always is made of "==". If you use "=" this is an assignment.

For example:
$a = 1;
if ($a == 1) {
echo "A is 1";
}

In the example above, first we set the variable $a to 1. (A variable always stats with a dollar-sign). Then we use an If statement together with an equation to see if $a actually does match 1, if this is the case (and it is), we show the user: A is 1.

$a = 5
if ($a = 1) {
echo "A is 1";
}

The example above looks the same, and to the untrained eye, you would expect that the application would show nothing to the user, since we have set $a to 5 and not to 1, so the If statement should not execute the echo code.
However, the example above does not contain an equasion but an assignment. And an assignment always return a boolean true when the assignment was succesfull. Since this is almost always the case, the if statement will receive a boolean true as input and will execute the code, even if $a is not 1.
Note however, since we have done an assignment on $a, for all the code behind this if statement, $a will be 1!.

Other equasions that can be used are:
  • $x == $y - does $x match $y ?
  • $x != $y - does $x NOT match $y ?
  • $x < $y - is $x smaller then $y ?
  • $x > $y - is $x bigger then $y ?
  • $x <= $y - is $x smaller then or equal to $y ?
  • $x >= $y - is $x bigger then or equal to $y ?

Step 5

So how are we going to handle this ?
First we would need to know what time of day it is. The date function can handle this for us.
We only need to look at the hour, so lets ask the date function to get the hour and we'll put it in a variable.

$hour = date("H");

Keep in mind that this will return the hour in 24h format. So 1PM is 13, 10AM is 10, 11PM is 23 and so on.

Now, we need to write some if statements and look if the current time is bigger then the time from where we would start to say goodmorning, goodafternoon, goodevening and goodnight.

We know that the clock starts at 0h (midnight). So lets set our greeting variable to goodnight.

$greeting = "Goodnight";

Now we can write if statements to see if $hour is larger or equal to 6am (goodmorning), noon (goodafternoon) or 6pm (goodevening).

if ($hour >= 6) {
$greeting = "Goodmorning";
}
if ($hour >= 12) {
$greeting = "Good afternoon";
}
if ($hour >= 18) {
$greeting = "Goodevening";
}

So now, from 0 to 6, $greeting will be "Goodnight", from 6 to 12, $greeting will be "Goodmorning", from 12 to 18 it will be "Goodafternoon" and from 18 to 0 it will be "Goodevening".

Now we can adjust our Hello world code so it includes the greeting:

< div align="center" >< ?= $greeting; ? > World! Today is < ?= date("F, ds Y"); ?> < /div >

This would then display for example:
Goodmorning World! Today is January, 27th 2010

Let me show you the full code now:

< ?php
$hour = date("H");
$greeting = "Goodnight";
if ($hour >= 6) {
$greeting = "Goodmorning";
}
if ($hour >= 12) {
$greeting = "Good afternoon";
}
if ($hour >= 18) {
$greeting = "Good evening";
}
? >
< html >
< head >< title >Hello world! < /head >
< body >
< div align="center" > World! Today is < /div>
< /body >
< /html>

So now you have written a simple script that gives a timebased greeting and tells you what day it is.
Keep in mind that the time is server based. This means that if the server is located in London, it will be on London time. So while it might be morning on the westcoast, the time difference of 9 hours would make it evening on the server in London.

Tips & Warnings

You can check out www.php.net which is mirrored around the world. It has a very big and usefull library of functions. Just type the function you would like to know more about in the searchbox and you will get a full description about what the function does and how to use it.

Advertisement
Advertisement

Comments

Add a new comment - No HTML
You must be logged in and verified to post a comment. Please log in or sign up to comment.

Explore InfoBarrel

Auto Business & Money Entertainment Environment Health History Home & Garden InfoBarrel University Lifestyle Sports Technology Travel & Places
© Copyright 2008 - 2016 by Hinzie Media Inc. Terms of Service Privacy Policy XML Sitemap

Follow IB Technology