... where I continue the forgotten series attempting to teach the basics of programming in php to people ...
Php - Control Structures
If a program could only work linearly, going through line by line, it would be pretty useless - it could only ever do one job.
Making it be more interesting relies upon three types of structure. Branching, looping and functions.
Branching Out
A branch is a type of structure that basically says 'either do this, or do the other' depending upon some condition.
For this we use 'if' - an example:
if ( $cashInPaw < 0 ) {
print ( "No! I'm in debt! Must... reach... atm...." );
} else if ( ! $cashInPaw ) {
print ( "no money. Bum" );
} else {
print ( "Yay! I have $cashInPaw left" );
}
Ok, that's a noddy example, but you get the basic idea there.
The squiggly brackets { and } are used around 'blocks' of instructions - without them you would need to have the 'if' before each and every instruction you wanted to do.
The < is used in the obvious way to mean 'less than' - and the ! means 'not' This is a bit of shorthand, used like that it means 'is this equal to zero' - we could quite happily replace that line with } else if ( $cashInPaw == 0 ) {
and it would do exactly the same thing.
Loopy Fruits!
There are LOTS of ways to do something repeatedly. The most obvious is:
doSomething();
doSomething();
doSomething();
...
Which will work, but can get a bit long to type. Further, it won't worrk if we don't know how many times to do it.
We could use branches to do it, but you'll soon see why that's a bad idea:
if ( $numberOfTimesToDoIt > 0 ) { doSomething(); }
if ( $numberOfTimesToDoIt > 1 ) { doSomething(); }
if ( $numberOfTimesToDoIt > 2 ) { doSomething(); }
if ( $numberOfTimesToDoIt > 3 ) { doSomething(); }
...
Even MORE to type, and what happens if you need to do something more than 3 times? Or more than 99 times?
This is where we use one of any number of loop structures:
do / while
do {
doSomething();
$numberOfTimesIDidIt++;
} while ( $numberOfTimesIDidIt < $numberOfTimesToDoIt );
while ( $numberOfTimesIDidIt < $numberOfTimesToDoIt ) {
doSomething();
$numberOfTimesIDidIt++;
}
THere is a subtle difference between those two ways of doing it - the first way will ALWAYS doSomething() once - whereas the second way can handle $numberOfTimeToDoIt being zero (or less)
Also you MAY need to set $numberOfTimesIDidIt to zero before the loop, best to anyway just to be safe.
A loop, a loop, my kingdom for a loop
This is the most flexible of all the loop commands - you can write whole programs that are just a for loop. But let's not bother with that, let's just get 'em to work.
for ( $numberOfTimeIDidIt = 0;
$numberOfTimesIDidIt < $numberOfTimesToDoIt;
$numberOfTimesIDitIt++ ) {
doSomething();
}
Ok, the first line inside the for ( )
happens once, before the start of the loop. The second line happens as a comparison every time round - if it's true then if does what's in the loop { }
And then it does the last line. It goes back, does the comparison, and so on.
Very flexible, but you'll almost always be using it like that - as a simple counter.
Functions
I'll refrain from following every other manual, and pointing out here that you've been using functions a lot already 'without even realising it' because that's too patronising even for me.
A function is a way to bundle up a bit of code that happens a lot, and save you from repeating it over and over.
For example, maybe you want to doSomething a lot in your program.
function doSomething ( ) {
// in here, we do stuff!
}
...
doSomething ();
Um. Noddy examples indeed. doSomething, ironically, doesn't do anything.
print()
is another example of a function, but it takes an argument.
function addTwoNumbers ( $firstNumber, $secondNumber ) {
// Oh boy, another noddy example
$addTwoNumbers = $firstNumber + $secondNumber;
}
...
$total = addTwoNumbers ( $subTotal, 4 );
// This adds four to the subTtoal, and puts it into total
Notice that this allows you to rename variables temporarily - while you're inside addTwoNumbers $firstNumber makes sense, but $subTtoal does not. And vice versa whilst outside.
Further notice that you set the return value by treating the function name as a variable.
Bg example
Something semi-not-noddy! Let's sing ten green bottles...
function singBottleVerse ( $numBottles ) {
// oh boy, I'm tempted to talk
// about recursion now. Kill me
if ( $numBottles < 1 ) {
print ( "There'd be NO green bottles...\n" );
} else if ( $numBottles == 1 ) {
print ( "There's be one green bottle...\n" );
print ( "One green bottle...\n" );
print ( "and if one green...\n" );
} else {
print ( "There's be $numBottles green...\n" );
print ( "$numBottles green bottles...\n" );
print ( "and if one green bottle...\n" );
}
}
for ( $bottles = 10;
$bottles > -1;
$bottles -- ) {
singBottleVerse ( $bottles );
}
Exericise for the reader
Assuming that $price[$entry]
and $quantity[$entry]
are the price and quantity of item '$entry' on your shopping list, total 'em all up.
And because this is a noddy test, you have to use a function! Ahahaha, that'll teach you to make me write noddy examples!
Edited by Vitenka at 2001-07-03 12:12:03