Thursday, November 29, 2012

Introduction to PHP for Website Developers

What is PHP? I could just have you visit php.net and find out for yourself, but although you would find out a lot of information about PHP you probably wouldn't really understand its use/purpose.

When you create a website using just HTML and CSS, it's "static". This means that no matter who accesses each page, all of the pages will look the same.

The reason for this is that with static web pages, the server simply regurgitates the same page (code) to each viewer. In most cases, you are going to want something better: a dynamic web page.



Before I go into more detail, you should know that you are expected to understand the basics of HTML and web pages in general. If you don't, you can read one of my previous tutorials found here.


Take a look at these two example pages I've created to see the difference - note that I've also placed each page's code at the bottom of the page (in bold) for your convenience:

http://thecodingwebsite.com/tutorials/phpintro/htmlpage.html
http://thecodingwebsite.com/tutorials/phpintro/phppage.php

Go ahead and try out both pages and read what's on them. I'll explain the PHP in more depth when you're finished.


So, right in the middle of the HTML code, I placed this:

<?php

echo "[b]Date and time: " . date(DATE_RFC822) . "[br/][br/]5 + 2 = " . (5 + 2) . "[/b]";

?>

The "<?php" and "?>" are what signal the server to start and stop processing the code as PHP rather than HTML. Whatever is in between these two tags will be executed as PHP code on the server every time a page is requested/refreshed.

You can have as many start and stop tags as you want on a page and you can place them wherever you want on a page as long as you're in HTML whenever you place a start tag and you're in PHP whenever you place an end tag, etc.

When the "echo" command is used in PHP, it signals to the server to output HTML code at that spot in the page. So, when you visit the example PHP page I made above, rather than seeing the PHP code you're actually seeing the resulting HTML code that is output by the server after it has executed the PHP code.

As a simpler example, if you place this code in a page:

<?php

echo "Hello world!";

?>

It will output (without quotes): "Hello world!"

If you want to concatenate (add) two strings (pieces of text that are surrounded by quotes) together, simply place a period "." in between them:

<?php

echo "P" . "H" . "P";

?>

This will output: "PHP"

Lastly, you can call PHP functions, do PHP math, etc. As far as the PHP code in my example goes:

echo "[b]Date and time: " . date(DATE_RFC822) . "[br/][br/]5 + 2 = " . (5 + 2) . "[/b]";

I'll separate each piece by a line so that you can see what's being done more clearly:

"[b]Date and time: "
date(DATE_RFC822)
"[br/][br/]5 + 2 = "
(5 + 2)
"[/b]"

The first, third, and fifth pieces are strings of HTML code and text - they get outputted exactly as you see them.

The second piece is a call to the PHP date function which returns the current date/time. The fourth piece is a simple math operation - I added 5 and 2 together, so PHP outputs a "7" there:

Date and time: Thu, 29 Nov 12 01:03:55 -0500

5 + 2 = 7 

There are thousands of much cooler things you can do with PHP, but when I first learned of how HTML and PHP code can be used interchangeably like this and of PHP's capabilities in general (encryption/decryption, server file read/write access, database access, math, loops, arrays, functions, etc.) I was amazed. Hopefully you are too, because there is so much more to learn about and use with PHP!

No comments:

Post a Comment