PHP Lesson 1: Beginning

All web design discussion, including Ultimate Quiz MOD support.

Moderator: CricketMX Forum Moderators

Post Reply
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

PHP: Hypertext Preprocessor

I could bore you with indepth details about how it runs, what versioning system it uses etc, but I don't want to bore you :)

So I will sum it up by saying that PHP is one of the most popular and reliable server side programming languages in existance and that it has been proven by the sheer number of large programs written in it. (ie. vBulletin, phpBB etc)

Most Webhosting Companies, including Diagemshosting, Canaca, iPowerweb and the many other millions use PHP, so don't worry if you want to run a PHP-driven website. It's pretty easy to find a host to suit your needs. I'm getting off topic here, so if you are looking for a good webhost, I recommend searching at http://www.webhostingtalk.com.

You must always call a page written in PHP, *.php. Where * is a short description of what the file does, or anything that you understand. Say you were making a Quiz program, you could call your main file quiz.php.

PHP can be as basic or as complicated as you want it to be, with an infinite range of different functions, many included (others you can make yourself) and it's versatility there is next to nothing you can't do with PHP.

The most basic of these functions, is 'echo'.

Every php page, begins with:

Code: Select all

<?php
Or in some cases

Code: Select all

<?
A file of PHP code always ends with

Code: Select all

?>
I always use <?php as it has never failed me. Throughout files you may often see two slashes (//) or something like this:

Code: Select all

/*
* To do, or not to do, that is the question
*/
They are called comments, a programmer can add these anywhere in between the <?php and ?> to show what a certain area of code acheives or to simply state how they are feeling at the time. Examples:

Code: Select all

//
// Escape any quotes in the site description for proper display in the text
// box on the admin page 
//

Code: Select all

// I'm hungry
So we have learnt about starting and ending files, as well as comments.
Now you should have something that looks like:

Code: Select all

<?php
// I'm hungry
?>
Doesn't look very good does it, that is because it doesn't do anything. Comments do not at all affect the code.

Common types of comments are the double slashed type (//) and the asterisk type (/*)

The asterisk type must be opened and closed:

Code: Select all

/* 
to open

Code: Select all

*/ 
to close. Anything in between is ignored. If your confused it's ok, the double slashed type is much easier;

The double slashed type is the simplest, you simply type two slashes, and add your comment. Done. They do not need to be 'closed' in a sense, like the asterisk type do.

We touched on 'echo' a little earlier. With echo, you can print anything you want. Not print as in, print a page through your printer, I mean print on to your browser screen!

With simple code like:

Code: Select all

echo 'Hello World!';
your browser will print:
Hello World!
No 'echo' or the apostraphe or semicolon. That is because they are required for it to work. All lines of code must end in a semicolon, that is a given. However, with the apostraphe, this can be tweaked. These are used so PHP will know what exactly to display. You can use inverted commas too (" ") but the apostraphe is more secure when it comes to Database queries and advanced code.

So our file now looks like:

Code: Select all

<?php
// I'm hungry
echo 'Hello World!';
?>
You can replace Hello World! with anything you want though, maybe

Code: Select all

echo 'I want food';
You can add as many echo's in as you want, so your file may look like:

Code: Select all

<?php
// I'm hungry
echo 'Hello World!';
echo 'I want food';
echo 'I might buy some Nachos.';
?>
Note: See how I have ended every line with the semicolon

The code above displays the following in your browser:
Hello World!I want foodI might buy some Nachos.
Too messy, so you can use HTML in your code. You can use any code you want, I will explain in the next lesson, at the moment all you need to know is <br> (linebreak, or paragraph).
By putting <br> in your code it will skip a line between your text to be echoed. Confused? Here's how:

Code: Select all

<?php
// I'm hungry
echo 'Hello World!';
echo "<br>";
echo 'I want food';
echo "<br>";
echo 'I might buy some Nachos.';
?>
And that will display:
Hello World!
I want food
I might buy some Nachos.
Play around with it, add more lines of <br>, remember the semicolon and apostraphe rules still apply when using HTML.

Note: If you are using HTML, you must use inverted comma's around the <br> ("<br>") or else PHP may display the HTML code. This is to distinguish between what you want displayed, and what you want to be run in the background. If the text is enclosed by apostraphes, PHP thinks you want that to be displayed in the browser, not to be run. This applies for PHP variables, HTML and other code you may wish to run.

To finish, our file looks like:

Code: Select all

<?php
// I'm hungry
echo 'Hello World!';
echo "<br>";
echo 'I want food';
echo "<br>";
echo 'I might buy some Nachos.';
?>
Let me stress again, you must enclose HTML code in inverted comma's ("<br>") or it may not run correctly.

Good luck, play around with the code, replace the text and the comments, and if you are comfortable, the HTML code. Attach the files in a PM or Email to me if you want to see what it looks like live on the internet.
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
User avatar
Rat
Drain Brain
Drain Brain
Posts: 4476
Joined: Mon Jun 14, 2004 9:38 am
Location: in the dark

Yay. I was wondering when the php course would start. Thanks battye.
Post Reply