This chapter is an important one, we will covers control stuctures. Control structures allow us to control our scripts, to perform different statements, output different output depnding on certain conditions, to perform loops and so forth. Here we’ll have a look at a couple of them.
ConditionalsConditionals allow us to perform different statements depending on certain conditions, here is the formal for a simple if statement.
It kind of speaks for itself. The
This allows us to print out something if the condition is NOT true also. So if it is true it will execute whatever is between the curly braces for the if() statement. Or else it will execute whatever is between the curly braces for the else statement. There’s more, we can use
This time we used the == operator. If $a is EQUAL to $b, do whatever is in the curly brackets, ELSE IF $a is greater than $b, do whatever is in those curly brackets, OR ELSE do whatever is in the final curly brackets.
Conditionals are very useful and it is essential you know how to use them, have a play around.
WhileWhile is a function that allows us to perform actions
This process repeats until
While is a very useful control structure, if you get deeper into PHP and start working with databases and text files you will use it a lot. Have a play around with it.
Putting a condition inside the () after the while statement that will always be true (like 1==1) is not a good idea. It will produce an inifite loop.
MoreThe two control structures I have showed you are the most commonly used. There are more but I don’t want this chapter to be too long. So rather than repeating what is already out there available to you, I’ll just direct you to The PHP Manual: Control Structures. There you will find information on all the control structures and exhaustive directions on how to use each of them.
Go on to the next chapter to learn how to get deeper into PHP.
No Comments for this page.