If you are a beginner to PHP, learning how to use control structures is a must. In this article Joel will assist you guys by providing coherent examples.
If you are a beginner to PHP, learning how to use control structures is a must. In this article Joel will assist you guys by providing coherent examples.
Control structures are beneficial when coding in PHP. Many tasks can be accomplished with a small amount of coding but basic understanding on how to use them will solve many coding problems.
What are Control Structures?
Control Structures are often referred to as Conditionals, Branching or their familiar name, "Loops".
Loops are great for retrieving information from databases, constructing tables and building dynamic pulldown menus.
Here is a list of the different types of conditionals in PHP:
if/else
if/elseif/else
do/while
for
while
foreach
The "if/else" conditional is the most commonly used statement in PHP coding.
View the following code:
<?php $number ='5'; if($number =='3') { echo "The number is equal to 3"; } else { echo "The number is equal to $number"; } ?>
Explanation of the code:
The $number variable sets it's value to 5.
If the $number variable is equal to 3 and becomes true the echo language construct displays the number is equal to 3. This statement is FALSE.
Since the $number variable is really a value of 5 then it continues until it reaches the end of the conditional and displays The number is equal to 5. This statement is TRUE.
Extending the conditional with the "if/elseif/else" statement would allow more options.
View the following code:
<?php $number = 7; if($number == '5') { echo "The number is not equal to 7"; } elseif($number <> '9') { echo "The number is not equal to 9"; } else { echo "The number is equal to $number"; } ?>
Explanation of the code:
The $number variable sets it's value to 7.
If the $number variable is equal to 5, the echo language construct displays that its not equal to 7. This statement is FALSE.
If the $number variable is not equal to 9, the echo language construct displays it is not equal to 9. This statement is TRUE.
The second conditional is the true statement of the script and the script terminates.
Lets turn to "Loops" starting with the "for" conditional. The basic structure will continue looping until it resolves to FALSE.
Here is an example of the "for" conditional that builds a pulldown menu with the years specified by the variable $x and the operand on the right.
An include file holds functions and database related information used by the script. The script connects to the database and selects the table.
The query selects the distinct column and narrows it down to the specified rows of the table.
The query values are set and the form is created using the column name derived from the SQL commands.
The "while" loop creates the pulldown menu and the row values are set.
Creating a pulldown menu that contains the months of the year would be bothersome using HTML. Using an array to hold the information and the "foreach()" statement helps speed things up in the long run.
An array is created with the names of all 12 months.
The form is constructed with the method and action.
The top position on the pulldown menu, labeled "Pick a Month" is created using a forward slash and it's value is set to null.
The foreach construct extracts the values of the $month variable and produces the pulldown menu with each month's name.
The curly brace helps exit the loop and the closing tags finish the form.
I hope this gives you an understanding of Control Structures and how to use conditionals to your advantage.
Happy coding and remember, "Let the code do the work".
Copyright 2003 - Written by Joel Philip - http://www.phpcollege.com
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.