Basic PHP Control Structures
This article continues from PHP Variables Primer. At this point we will add something that will actually allow you to use what you know to make simple, useful PHP scripts. I'm going to call them control structures. There are quite a few handy control structurs in PHP but we will only cover the FOR loop and IF along with ELSEIF and ELSE as this tutorial is supposed to be just the basics. Check out the PHP code below...
for ($variable=0; $variable<10; ++$variable){
echo "output this line!<br />";
}FOR Loop
The above code is what is known as a loop. The code between the "{}" is a code block. In this case we have a for loop with an attached code block that will execute every time the loop executes. Notice the $variable that we use. This can be anything you want really as the first part, $variable=0;, sets $variable to 0. The next part is the condition for the loop. I will assume you know the greater than and less than symbols from gradeschool(<>). If you don't then brush up on them as I will not cover them in detail here.
Basically $variable<10; means that as long as $variable is under 10 the FOR loop will continue to execute. The next part, ++$variable, tells PHP to increase $variable by 1 every time the FOR loop executes. So $variable is set to 0 and then we check to see if the loop should go on. If 0 is under 10 then we proceed, adding 1 to $variable which would now make $variable equal to one. We start again asking if $variable is still under 10. If it is then we repeat the previous steps until $variable isn't less than 10. At that point the code block (code in between the "{}"s) will not be run again. The above code will produce the following output...
output this line! output this line! output this line! output this line! output this line! output this line! output this line! output this line! output this line! output this line!
IF ELSEIF and ELSE
Hopefully you understood the whole loop thing above. We will quickly go over IF, ESLEIF and ELSE. This shouldn't be too hard to understand if you are still with me and if you do get it then by combining this with variables and the above FOR loop you should have a decent start on really sinking your teeth into PHP.
Check out the code below to help us get a grip on the IF control structure...
if ($variable=="nifty"){
echo "The variable=nifty!";
}
if ($variable<5){
echo "The variable<5!";
}
if ($variable>10) echo "Over ten!";
if ($variable!="nifty"){
echo "It isn't nifty!";
}Just as in the FOR loop examples everything between the "{}"s is a code block. In these cases the code blocks are only executed by PHP when the conditions of the IF statement are true. In the first IF statement if $variable is equal to nifty then the condition is true and the code block is executed. The reason for the double "=" in the condition is that in PHP the "=" sign is for assignment where the "==" is for comparison. The statement if ($variable="dog") echo "dog!"; will always be true because it sets $variable to "dog" in the IF statement instead of comparing the two with "=" as it should. This is a common mistake for someone new to PHP to make. You will get used to finding and fixing this mistake in your code as you progress.
You probably recognize the greater than and less than symbols ("<>") from before but you may not recognize the "!=" in the last IF statement. I snuck this one in as it is a very handy operator. "!" basically means "not" so "!=" means "not equal to". This statement is true only if $variable isn't equal to "nifty".
In the third IF statement you probably also noticed that there are no "{}"s around the code. If the code you want executed for an IF statement is only one line then you can tack it onto the end of the statement. The easy way to understand this is to note that there isn't a ";" after the above code blocks. PHP knows that the line after a code block is seperate from the code block already because it isn't in the code block. PHP always needs something to do if your condition is true though. If you can do everything you need to do on the same line with the IF statement then a code block isn't necessary. The ";" tells PHP that the IF statement has ended and the next line will begin a new statement.
Conclusion
Put all of this together and you should have a good start. This tutorial was meant as a starting point. Look up other tutorials that you can understand and make sure to check out the links in the resources box on this page. I hope this was helpful to someone. Keep checking back as I am constantly adding new tutorials and such.


Leave a Reply: