PHP – The Basics; Part 2
Written by Owen Jones
Welcome back, in this tutorial I shall be teaching you about Comments, if Statements and Switches. If you haven’t read my first tutorial I recommend you do to get up to speed (link).
Still with us? Okay; let’s get started!
Comments
Comments are one of the most important things any coder can have in his/her arsenal. It’s what helps keeps a script readable, so when you come back to it in however-many-weeks time, you still know exactly what’s going on.
In PHP there are two main types of comment, Single-line and Multi-line.
Believe it or not, single-line comments go on a single line (gasps of ‘oh really’ I hear). These are mainly used for code readability and make the code easier to follow. Single line comments can either start with a double forward slash (//) or a hash (#), the comment then follows. I’ll use a small snippet from a CMS I wrote as an example;
Example;
// If this class has not already been run.
if(!defined('BASE_RAN')) {
// Set instance (for function return)
self::$instance = $this;
// Flag that this class has been run.
define('BASE_RAN', true);
}
As you can see these comments enable you to easily follow the code, making it easier to debug. Multiline comments operate in much the same way, but are for larger pieces of information, usually information about the current script, of an upcoming function. Multiline comments must start with /* and end with */. There is no limit to how long they can be.
Here are two examples;
/* query * Usage: Initiates a mysql_query. * @param mixed $query * @parem boolean $return * @return false or query result */
NB: the asterisks (*) at the beginning of the lines inside the comment are not necessary, I just add them to neaten the script up. That’s just about all there is to comments, hopefully you can all go out and make your code prettyful =]
Now onto Conditional Statements
One of the main things when creating a script is being able to control the scripts flow. This can be done by using what are known as conditional statements. These basically ask questions, and the answer changes which route the program will take. The standard conditional statements are the if, else and else if statements. Here is an example of using the if statement.
if(1=1) {
Print “This is true”;
}
if((8/2) == 4) {
print “This is also true”;
}
If((10-5) != 5) {
Print “This just ain’t true”;
}
This is basic usage of the if statement. It isn’t a brilliant example because the conditions (the question being asked) will always give the same answer, so let’s start using something that could change; a variable! Take the next example for instance, by changing the variable from red to say, blue, we can change what happens.
$theColour = ‘red’;
If($theColour == ‘red’) {
Print ‘The Colour is Red’ ;
}
Else
{
Print ‘The Colour is something other than Red’;
}
Hopefully you can see how that works and what the else statement does. The else if statement is very much like the else statement. If you look at the if structure below you should be able to see the similarity.
If($ourVar == ‘bob’) {
Print ‘It’s Bob’;
}
Else if($ourVar == ‘pete’) {
Print ‘It’s Pete’;
}
Else
{
Print ‘Yikes! I don’t know who it is’;
}
It’s an if statement cross-bred with an else statement and if you are able to master conditionals your programming capabilities are greatly expanded.
Just a small note; if you look at the text ‘It\’s Bob’ you’ll notice the back-slashed-apostrophe inside. The back slash is there to cancel out the apostrophe in the text, if it wasn’t there the string would end at the t of it’s and cause an error. The back-slashed-apostrophe keeps our code error free and our grammar correct (Yes, I’m a Grammar Nahtzee)
The only problem with else if statements is that unless you’re only going to use a few then your code becomes bloated, and badly structured. But my children, I have a solution…
Switches
Are simply an easier way to do lots of else if statements. With these there is very little explaining to do so I’m going to throw an example at you and explain it a little.
$ourVar = ‘pink’;
Switch($ourVar) {
Default;
Print “It’s neither pink nor blue!”;
Break;
Case “pink”:
Print “It’s Pink”;
Break;
Case “blue”:
Print “It’s Blue!”;
Break;
}
As you can see we define a variable, then a switch. In a switch each ‘else if’ is called a case, the ‘default’ is our else. Anything that doesn’t fit into the other cases goes to here, you can of course just omit the default case, it’s not a nessesity. Each case must end in a break;, this tells php that were done with that case.
I don’t think there’s much more I can say.
Enjoy!
Next time: Functions, Arrays and Loops (Fingers Crossed)
Owen Jones is an Advanced PHP coder. Find out more about him @ http://owenjones.net
Tags: Amazing, Code, Coder, Comments, Owen Jones, PHP, Querys, Switches


No comments yet.
Post