So You Want PHP, Huh?

By Vince Barnes

Introduction
Hello, This is a one off tutorial, I wanted to try my hand at writing one as I was often annoyed at others I'd read, they often introduced things that simply hadn't been covered without explanation or gave a brief overview on something far too short to get a grip on. If you like this tutorial or think I've missed something out or failed to explain something properly please Email me. If I get positive response I might write another one.

Before we start
This Tutorial is aimed at people who already have access to php, hopefully your host has php set up on your system and you can start writing right away. If so good, otherwhise bug them to set it up sharpish or find a better host. Alternatively you could try setting it up locally on your machine (English: Downloading A server that can be run offline on your windows or linux operating system) Apache would work for this. However that is a little advanced for this tutorial, but if you want to give it a go there are some good tutorials on doing just that over at ScriptSearch under php/Tutorials. For now we're going to assume you can run php scripts, php scripts are saved with the file extension .php or .php3 depending on what version of php your running. Ask your host, if their running the current version (and they should be) of php version 4 then a .php extension will be used otherwhise .php3.

Php Tags
Php tags are inserted right into html code with <?php and ?> Tags, like so.

<BODY>
<?php
print "Php rocks";
?>


You can switch in and out of php code at any time. We've also just introduced our first comand 'print' which will be explained next.

Print
The print statement is the most simple to understand, it does exactly what you would expect, it prints "php rocks" to the page. This example is a bit stupid in that it could be done without php very easily and quicker but it's a good starting point. Note two very important things
1... "Php Rocks" is in quotes. This is important always put your strings in "quotes". You can also use commas 'Php Rocks' for literal strings.(ENGLISH: Literal means that it will print what's in the 'quotes' exactly as you put it. Non Literal with "double quotes" will convert 'VARIABLES' to their values. Variables are coming next'. A 'String' is just some text so with the comand print 'Php Rocks' you are Printing the Literal String Php Rocks' Got it? oh well come read this bit again after I've introduced you to variables.
2... the semi colon (;) at the end of the print line. All php lines should finish with a semi colon or they wont opperate properly. This can be hard to remember but php throws relivent errors usually if you try to run a script with errors in it so you can find them. the ; actually goes at the end of each statement, a statement being one comand. So this would also work.

<?php
print "Php rocks"; print "PHP ROCKS THE HOUSE";
?>


Variables
Variables are great, In as simple an explanation I can think of they are just bits of data that you can use anywhere in your script by just refering to their name. A Variable is always identified with an $ symbol. This makes sure the php engine understands that it's got a variable to handle and not just normal text, if you want to display a $ character in your text you'd use an Escape Character like this \$ The \ Escapes a symbols normal value. Don't worry too much about this right now. Anyway An example would probably be best at this point.

<?php
$rocks = "Php Rocks"; print "$rocks";
?>


Will display...

Php Rocks


Now would be a good idea to get accross that 'literal' idea. Consider this code with the print comand using single 'quotes'.

<?php
$rocks = "Php Rocks";
print '$rocks';
?>


Will Display

$rocks


You'll note that php didn't convert $rocks to it's value. That's what a literal string does, it's useful if you want to print something that doesn't use Variables in it. You'll probably understand the point to that more as you get into adding html via print. So Far we've only talked about the 'string' variable types which have text inside them, you can also use Number types or Integer if you want to make it harder to understand ;) Number types don't take 'quotes' or "double quotes" unless you want to print a number and not change it's value. Again examples are best for this kind of thing. Consider this.

<?php
$rocks = 100;
print $rocks;
$rocks = $rocks + 50;
print "<BR> Rocks now contains the number $rocks";
?>


Will display..

100
Rocks now contains the number 150


This just got a tad larger and may confuse those of you who are slow (like I was ;)) Line One $rocks is first assigned the value of 100 (the = sign always 'assigns' values if you hadn't worked that out already)
Line TwoWe then print the variable $rocks which displays 100 on the html page. You'll notice we didn't use any "quotes" with that print statement This is because we were only printing the number and not any text therefore it wasn't needed.
Line Three We 'Changed' the value of $rocks. This is a new concept but is the reason variables are so great. $rocks now equals $rocks plus 50... As $rocks was equal to 100 before it is now equal to 150
Line Four And finally we print the new value, this time we HAVE used quotes as we wanted to print a string and add the number inside it. We've also added a <BR> Tag in there so that it appears on a new line when rendered in HTML.

Hopefully your still with me. I'm going to give you a complicated example but it wont use any new techniques try to keep up with it. I'm not going to analyse it for you I'm hoping you'll understand it and therefore feel more confident in keeping up with it all, if you don't just go back and read what has been said thus far and examine the code.

Anyway...

<?php
$number=100;
$number2=99;
$string1="Hello";
$string2="And Welcome to";
$number3=$number - $number2;
$string3="This php tutorial, it is my $number3 st Tutorial";
$finalstring="$string1 $string2<BR>$string3";
print "$finalstring";
?>


Will Display..

Hello And Welcome to
This php tutorial, it is my 1 st Tutorial


Tiny note. You'll see I used the - (minus character) Devision and multiplication on keyboards is done with the / and * characters respectfully

While
We're going to introduce a slightly more complicated example now. The 'WHILE' loop. Looping is that great thing in programing languages. It allows you to display vast tables or masses of data with very little programing. The While loop works like this. While Condition is true Do this stuff... You may be wondering what being 'true' means. Everything you do in php or any programing languages returns a value of either Ture or False. Usually this is true. If you were to assign nothing to a variable it would be FALSE however if it contains anything else it'll be true with the exception of the number 0. Have a good look at the next example and I'll go into detail on it afterwards.

<?php
$number=1;
while($number < 10){
print "$number<BR>";
$number = $number + 1;
}
?>


will display..

1
2
3
4
5
6
7
8
9


Now the new concepts. Take this bit.
While($number < 10) Now we assigned the value of 1 to the variable $number just before this. Basically what that piece of code is doing is saying While $number is LESS THAN 10 do this... Needless to say the < character is less than. You could also use > (Greater than) <= (Less than OR equal to) >= (Greater or eaqual to) and finally == (Equal to) Yes double == ONE = is used to ASSIGN two == is used to COMPARE. We'll show you some more examples on this later.
{ print "$number<BR>"; $number = $number + 1; } Now the { and } Characters are used to define what you do WHILE the condition is true. In this case we've made it print the number followed by the html comand
so we get each number on a new line. We have also Added one to our $number variable ($number equals itself plus one) so that the loop eventually retruns a value of FALSE and stops looping. Our number will increase and eventually reach a value of 10, at this point $number WILL NOT be less than 10 and the loop will stop. You could change ($number < 10) to ($number < 10000000) But this would probably crash your browser ;)

Now we're again going to show you a more complicated loop That will display something quite neat. A knowledge of html tables will be required to understand this, but if you don't know that then you really need to go learn this stuff before you advance to php. Anyway There will be no in depth explanation of this but it uses the exact same concept as above but looks a little more scary, just look closely and take on board what you have already learned.

Here..

<TABLE>
<?php
$number=1;
while($number < 11){
print "<TR><TD bgcolor=red>Hello</TD></TR>";
$number = $number + 1;
}
?>
</TABLE>



Will display this rather ugly thing

Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello


This is what makes programing on the web so exilerating. That table is created Dyamically, I.E. It was not put in by you, you just put in the rules (to print a new TAble Row ten times) and it created it on it's own. The Advantage of this fairly simple example (in terms of what you can do) is that suppose you want to change the background color to yello you just change its' value in the WHILE loop and all ten rows change you could add another <TD> to the comand and the entire table would change or you could change the word hello and all would change again. (you see how neat it is now?) You could alternate the table colors too but that requires some knowledge of the IF statement which is what we're going to cover next.

IF
If you have grasped the while concept properly this shouldn't be too hard to understand, if you havn't grasped the while concept please go back and REALLY read it, don't just look over it you gotta read what's being said or you'll never really know what I'm talking about. If you got while properly Then we'll proceed. IF is pretty much how it sounds, it works like this.
IF Condition is true Do this
It is incredibly similar to while but it only does the comands once. As ever an example would server well.

<?php
$number=1;
if($number == 1){
print "Number is equal to 1";
}
else {
print "Number does not equal 1";
}
?>


Here we've used the comparison operator (ENGLISH: we've tested wether $number is equal to 1) == to see if $number equals 1. If we'd used $number < 10 any number from 0 - 9 would have been true and the comands between { and } would have been executed but JUST THE ONCE. Again here if $number does equal 1 then we'll print that it equals that. Now you'll also note the 'ELSE' comand. This is used to do other stuff if the previous IF comand resulted in FALSE or not 1. This allows us to do a kind of Branching (doing different stuff depending on various things) thing. IF ELSE And WHILE (and other comands which I will not go into right now) use the { and } braces to signal what we want to do while/if/isn't TRUE. Just look at it and it should make sense. We're now going to expand our previous code to display the table but we're going to use IF and ELSE to make the colors alternate. Here we go.

<TABLE>
<?php
$number=1;
$alt1="red";
$alt2="blue";
$alternating=$alt1;

while($number < 11){

print "<TR><TD bgcolor=$alternating>Hello</TD></TR>";
$number = $number + 1;
if($alternating==$alt1){
$alternating=$alt2;
}
else {
$alternating=$alt1;
}

}
?>
</TABLE>

Will produce..

Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello


Okay so the colors are aweful but the beauty of it is you can change them at any time. You'll notice that the IF and ELSE were nested inside the while. You can do this as much as you like going as far as to put IF statements inside IF statements inside ELSE statements ;) Keeping track of all the { and } are possibly the biggest headache programers have to contend with, If you ever get confused just count the open ones against the closed ones and make sure they match. Basically what we did was define three variables. One for color A one for color B and a final one that changes each time this is the one we use as the table BG color. IF the current color of the final one is equal to color A then we change it to color B if it isn't then it must already be equal to COLOR B therefore we change it to color A. This will display alternating colors. Also note that we always put the <TABLE> and </TABLE> tags OUTSIDE of the loop as we only want to print these once, in fact we've printed them right outside the <?php and ?> Tags this way we don't need to muck about with the extra print statement.

Okay I'm knackered out. If you liked this tutorial then please Email me if I get enough positive feedback then I Will write a second part getting into more advanced comands. Also if you have any complaints with it, or questions or querys then also email me, I like getting email :) Just please don't send me a question that is answered here. As much as I enjoy recieving email I dont' really like explaining things over and over, however if it's something you just don't understand because I didnt explain it adequatly feel free to email and I'll do my best to help you out. Cheers.

Davy James Webmaster of NintendoEye.com


Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date