PHP : Beginners Guide PT.2

PHP : Beginners Guide PT.2 - All the details and information you need to get started with using PHP.

In category php.

Posted by Joseph Skidmore on 11.09.05.

Now we have covered the basics of PHP it would be a good idea to show you some examples and codes on how to create your own scripts.

So how shall we begin this mysikal journey? Well let's start by showing you how to turn a normal .HTML page into a .PHP page ... take this normal piece of HTML code;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; 
charset=ISO-8859-1" />

<title></title>

</head>

<body>

<p>Some text here.</p>

</body>
</html>

Nothing fancy, now take this code, paste it into Notepad (yes notepad!) then click on File > Save As. Save the file as "index.php". There you have it! Your very first php page. Click here and view an example of your very first php page.

NOTE: To view .php files on your home computer you will need a server, I recommend using EasyPHP which is a very useful tool. See my tutorial for how to download and install EasyPHP and the English language pack.

OK, you've got me! There is no php within that page is there, correct! But that's my point, you don't need any PHP scripts inside a page for it to have the .php extension. If there are PHP scripts present on a page then you NEED to have the php extension, take a look at this .html page with a PHP script inside.

As you can see with the above example the PHP code doesn't work correctly, whereas if you used the .php extension you would get these results: php page with php code present. By viewing the source of both this example and the previous one you will notice that with the extension .php you are not able to see the <?php echo ' php code that you could see that you could when the page had the .html extension.

Ok, thats enough of the easy stuff. Let me show you how to add PHP code to a regular .html page, using the same HTML code I posted above we will place the time and date on the page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1" />

<title></title>

</head>
<body>

<?php

$time = time();
$datetime = gmdate("n/j/y - g:ia", $time);

echo ''.$datetime.' GMT';

?>

</body>
</html>

That small piece of php code creates this: Echo the time onto the page.

To declare the start of a PHP code you use the tag: <?php and the end tag is: ?>. Very similar to HTML isn't it?

Ok, now this may be a small piece of code but it still takes some understanding to correctly learn exactly what it does. $time - This is called a variable ... but what really is a variable, I am not going to go all technical on you because you will learn nothing that way. Think of a variable as a box, you store things inside it, such as values (numbers, text, socks ... anything!), all variables start with the dollar sign '$'. So what exactly are we storing inside '$time'? Well the name of the variable should be a clue, we are storing the time. time() is a function which grabs the time from the server.

We use letters within the date() function to format the time so we can understand it. Here's what the letters mean;

  • g (lowercase g) - 12-hour format of an hour without leading zeros - 1 through 12
  • i (lowercase i) - Minutes with leading zeros - 00 to 59
  • a (lowercase a) - Lowercase Ante meridiem and Post meridiem - am or pm

And the last part of the code? The apostophie? This declares the end of a line in PHP.

So that part of the code gives up the time but what about the rest of the code. $datetime is another variable which is storing the value; gmdate("n/j/y - g:ia", $time);. Ok, gmdate returns the date from the server ... it can't be used on it's own the same way time can, which is why we have ("n/j/y - g:ia", $time) included within it.

All those letters mean something, trust me they do. Each letter brings back a different value, heres the breakdown list;

  • n (lowercase n) - Numeric representation of a month, without leading zeros - 1 through 12
  • j (lowercase j) - Day of the month without leading zeros - 1 to 31
  • y (lowercase y) - A two digit representation of a year - Examples: 99 or 03

All put together and separated by a forward slash combines to give you the date. For more information on the date take a look at the PHP Manual on the date function.

Now we have out variables set and packed with all this information how do we get it onto the page? This is where the echo comes into play;

echo $datetime;

Echoes the contents of the variable $datetime onto the page.

Echo essentially means show this on the page, when you include a variable inside the echo it will empty itself out and show you what's inside (like tipping our box upside down).

Page generated in 0.0016 seconds.

Whatcounter Free Invisible Stats CounterFree invisible hit counter