Well hopefully you all know your variables, arrays and data types - because this is the place that it all comes together ( and we can start programming something useful).
First, we have to understand conditions:
5==5;
Here, we test if 5 is equal to 5 - which is of course true. That is why that "condition" evaluates to boolean true.
10!=14;
Here we test if 10 is NOT equal to 14, which is ( again) true.
4>9;
Is 4 greater than 9? There are a total of 8 condition (or "comparison") operators: ==, !=, <, <=, >, >=, ===, !==, <>
So, the first few are fairly simple to understand: is equal to, is not equl to, is smaller than, is smaller than or equal to, greater than etc etc.
===, what on earth is that? Well its where you want to test to see if the data type is the same, not just the value:
5==="5";
That is false, because the integer is not the same as the string, even though they hold the same value ( an == operator in there would return true).
<> and != are the same, I personally prefer !=, however its up to you.
You can usually test almost anything with the conditional operators, however we usually use variables (or an embedded function call like this (assumes you have written an add() function):
add(5,10)==15;
So why do I need these comparison operators? Because not all programs run from A to B without testing something. For example (in a quite literal sense), you need to see if the lights are green before driving into the intersection. You need to test to see if the fruit is rotton etc. After all, a program is designed to do things faster than we do, to automate a proccess.... it needs to make decisions...
------------------------------------------------------------------
IF somethign THEN do somethign else.
if(5==5) {
echo "Its true!";
}
else if (9>=10) {
echo "This one is true!";
}
else {
echo "Everyone is lying today
}
Now of course the code inside the else if will never run - because its condition will always evaluate to false - thus not executing.
The else code, runs if nothing "else" was true
The thing to note about an if, else if, else structure, is that once one of the conditions are true, it will NOT run any of the others - even if they are true. If (always) the first IF is true, then the rest of the else if's will not be tested, and it will skip to the end of the "else" code.
You can put anythign you want inside these if, else if, else conditions, functions, variables, echo, other conditional statements.... ANYTHING! You can also have more than one else if, or no else if's. but you MUST! have the if() if you want to test anything.
else if() {} - by itself is invalid:
else {} - by itself is invalid ( well there are other times to use an else, however thats for later).
if() {} - is fine
------------------------------------------------------------------
SWITCH
This is an alternative to the IF statement. In most cases the IF is best, however SWITCH is designed for use when you are testing the same variable for a number of different values, such as the lowest common factor of a number: (assumes that you have written a function to find such a thing).
$number = LCF(19); // this will be 2
switch ($number) {
case "correct":
echo "Correct has executed";
break;
case 2:
echo "2 has executed";
break;
default:
echo "Nothign else was true, so this is running";
break;
}
Notice how I have placed break; at the end of each case. This is to stop PHP from executing anything else in the case brackets. If I left off the case, then everything else would be executed.
default does the same thing the else does, it runs if nothing else is true.
------------------------------------------------------------------
FOR (condition), execute code.
This loops through code, based on a counter. eg:
for($number=1; $number<=10; $number++) {
echo $number;
}
This displays the numbers 1-10
The contents of the FOR brackets is as follows (it is always 3 arguments).
$variable=value //initial value for the loop.
condition // This is the condition that is tested.
operation // This is the operation executed at the end of each loop iteration (each time the loop runs).
the ++ increments the value on the left, by one. if you want to increment it by another operator:
$number -= 5; // subtracts 5 each iteration.
$number--; // subtracts one each iteration, useful for a countdown loop
$number *=$number; // increments $number by the square of itself each iteration (1,2,4,16,25 etc).
------------------------------------------------------------------
WHILE something is true, then DO this:
This is fairly straight forward:
while (condition) {
execute code
}
This is similar to IF's and FOR's:
$array = Array("one", "two", "three");
$index = 0;
while($index<3) {
echo $array[$index];
}
This will display the contents of the $array variable (more specifically it will display the first 3 elements).
------------------------------------------------------------------
DO this WHILE condition
This is almost identical to the WHILE loop, except it will always run the code at least once. A while loop can ( depending on the condition) never run at all.
do {
code
} while (condition);
For example:
$incr=0;
do {
echo "Hello World!";
$incr++;
} while ($incr = 10);
Fairly straight forward yes?
-- WARNING -- infinite loops!!!!!
Watch out for infinite loops. These loops have no case where it returns false ( thus, running forever). In some applications ( such as GUI etc.) they are useful, however in almost all web applications they are horrible evil loathsome little cockroaches (not that I have anything against cockroaches of course). They will run endlessly until PHP's Maximum Execution Time has been exceeded, and the script is forcefully stopped. For example:
while (True) {
code
}
will run endlessly, because it is always True, so: WHILE true, DO code..... it is always true this never escapes.
So, how can we avoid this.... continue and break statements
A continue statement skips to the end of the loop, where the condition is tested again. This is helpful if you have an if() inside the loop, and you want to skip the rest of the code ( such as don't run the code $number is larger than 100). Granted, that the condition there could and probably should be tested in the initial condition.
A break statement "breaks" out of the nearest loop all together, and does NOT test the condition again. You saw this used in the case, where it breaks to the end of the case{}, skipping any other code.
You can call these statements just the same as in the case:
continue;
break;
So... It's all very nice to be able to test one condition, but what if I want to see if the apple is green AND I have enough money to buy it....
Well thats where we start making multiple comparisons. For those familar with C++ etc, && and || is available in PHP, and is indeed my prefered method. However for those that are more "english" inclined, your script can read very easily when you use the "and", "or" and "xor" comparisons :
$apple == "green" and $money >= $cost;
$apple == "green" && $money >= $cost;
These two are the same, one uses &&, and one uses ||. Note that if you were to use both && and "and" in the one test, the && would be first, which would be the equivalent of:
if(($apple=="green" && $price >= $cost ) and $apple_type=="big and juicy") {}
So first, if the apple is green and I can afford it ( which at the moment well assume is true). Then we test to see if that is true, AND the apply is big and juicy. This will eventually all evaluate to true.
The xor comparison is the equivalent of: "This is true or that is true, but not both". So for example you wanted to only buy an apple that is big OR red, but not BIG AND RED.
So if you wanted to test a form, to see if the entrant was male and over 13, then you would use:
if($sex=="male" and $age > 13){}
and if you wanted to test if the entrant was a male or a female but not both:
if($sex=="male" xor $sex="female") {}
The last thing Ill cover are two new functions that are part of PHP's internal function library: isset() and empty()
Often you will need to see if somethign is set, for example, has the user uploaded a file (Im not going to use actual variable names here because it may confuse some people):
if(isset($uploaded_file) && !empty($uploaded_file)) {}
This tests to see that the variable $uploaded_file is set, and that the variable is not empty ( or is not equal to NULL). This is the prefered method of testing, instead of if($uploaded_file).
I hope this lesson has made sense. I'll be posting the homework soon.
Enjoy!

Sign In
Register
Help


MultiQuote