When you are learning, it helps to be able to look over someone's shoulder!



PHP Lesson - 1
PHP - What is it?
Well the best way to explain PHP is to explain those other terms you'll come across.
- HTML - HyperText Markup Language - This is the language your web browser reads to determine how to display a webpage using a predefined set of Basic HTML Tags.
- CSS - Cascading Style Sheet - This is the language your web browser reads that will allow you to affect the appearance AND Layout of a website.
Now PHP - Pre Hypertext Processing is a programming language that runs on a webserver and allows it to make descisions on what HTML code is going to be sent to a vistors web browser. It effectively allows you to Create Dynamic Web pages.
An example
Say you have created a web page on your computer using a fancy web editor or just a plain text editor and you upload this file to your web server where your website is hosted using a FTP program of some sort.
That HTML code sitting up on the webserver cannot be changed. The only way you can change it is if you make some changes on the file on your computer and upload the changed file.
Any file that someone has created and uploaded as a .htm or a html is called a Static Page
When someone visits your website with a static web page, all the webserver does is send that file down to the visitors web browser exactly as it is. Simple
In steps PHP
PHP can make decisions depending upon what instructions it is given.
As a basic example say you wanted your webpage to display what day it was, Like Monday, Tuesday etc.
You can instruct the PHP server to go and find out todays date, turn it into a day name - or any format you like (like Day Month Year etc) and add it into your webpage and THEN send the final result to the vistors web browser.
Your visitor will see the webpage with the Name of the Day. Something you couldn't do with a static HTML page. You'd have to write separate pages for each day of the week and then somehow decide which page to send.
Javascript can do that too...
Javascript is another language that runs on the Clients computer. The Javascript code is downloaded along with the rest of the webpage and it then runs on the visitors computer.
Java Script has it's place in the world, it's commonly used along side with PHP but we wont be covering any JavaScript because, Well, I've not learnt any yet so I can't tell you much about it.
To date I stay clear of JavaScript only because some people have it disabled and if your site is heavily Javascript dependant, well things just wont work. Of course good programming practice says that you should always have a fallback so if the JavaScript dont work (being disabled on your visitors PC) then you can have some PHP in place to pick up the peices.
It does make sense to do what you can on the visitors PC than having the Server creating whole new pages each time something small changes - like a Countdown Timer or something like that. So JavaScript does have its place.
Ok back to PHP
So as a filename ending with HTML is a HTML File, a PHP File has a File Extension of PHP. That is a PHP file has the format of filename.php
An Apache webserver will be expecting to find a HTML File called index.html, It'll be expecting to see our PHP file called index.php
Hello World!
Hello world is something you get to learn how to produce in nearly every progamming language under the sun, so we'll show how it's done using PHP.
The Code
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="Generator" content="Kate, the KDE Advanced Text Editor" /> <title>Untitled</title> </head> <body> <?php echo "Hello World"; ?> </body> </html>
Ok you'll see that our index.php file is mainly normal HTML which the PHP Server will just ignore and send it out like a normal HTML page would be sent.
What the PHP Server is interested in is the bits between ANY Opening and Closing PHP Tags. Those being the <?php and ?> Tags.
Between those tags in our "Hello World" Example is a line that says echo which means Send or echo what follows. In this case it is a sentence, enclosed in Double Quotes which can also be Single Quotes with the words Hello World AND a Semi Colon on the end.
That Semi Colon character tells PHP that it is the end of the instruction.
Remember what you start with is what you finish with. If you start your string with a Double Quote - End it with a Double Quote , you can use single quotes freely in between and if you start with a Single Quote - End with a Single quote and you can use Double quotes freely in between
So we are asking the PHP server to send the words Hello World to our visitor along with the HTML that is in that file.
That's nothing too magic at the moment...
Remember...
A PHP file must have the file extension ( the bit after the dot) of php. Any PHP code in the file must be enclosed in the opening and closing PHP Tags - <?php and ?> and ANY Instruction must end with a Semi Colon (;)
If you can remember those basics, the rest is simple... well there'll be less problems as these things can be forgotten and poor ole PHP can get a bit confused causing all sorts of fun errors.
I'm sure programmers are always saying "Whoops, left off the semi colon" or "Whoops, forgot my php tags". I do sometimes in a flurry of keyboard typing...
That should do you for now...
That should be enough to get you excited. If you haven't yet taken a look at PHP Syntax Highlighting Script then I suggest that you do. It's got lots of PHP example code on that page...