PHP for the World Wide Web: Second Edition written by Larry Ullman
Chapter 6
Control Structures
"Writing conditionals in PHP comes down to identifying True or False situations." p132
Creating the HTML Form
As with the work in the preceding chapters, the ideas we will be learning and applying will need a form to work on/from. So an HTML form will be constructed to gather certain information from users which will then be sent to a PHP page.
The if Conditional
The syntax is quite simple:
if (condition) {
statement(s);
}
It depends on the True or False nature of the condition as to whether the statement is executed or not.
The Validation Functions listed on page 120 are what decide whether to return a True or
False concerning the data entered.
There are many, but these were used in the chapter:
- empty()function, good for noticing form boxes that were skipped
- checks to see if there is a value entered, which would return False
- if there is 0 or an empty string, returns True
- isset()function, almostthe opposite of empty()function
- returns True if a variable has a value(even 0 or an empty string)
- returns False if otherwise (I am still a bit unclear about this idea)
- is_numeric()function
- returns True if the variable has valid numbers
- integers, decimals, strings (ifa valid number)
- returns True if the variable has valid numbers
Using Else
if (condition) {
statement(s)
}else {
other _statement(s);
}
When using the if-else, the else statement(s) will be executed when the if condition is not met clearly and cleanly!
More Operators
Comparison:
- = means to assign a value to a variable
- == means that a variable is equal to a value
Here is link to the PHP Manual page with the Comparison Operators
Logical:
- Assist in creating more 'logical', apparent constructs
Here is a link to the PHP Manual page with the Logical Operators
Besides using Logical Operators to make more complex conditionals, Nesting Conditionals can be used.
When constructing Conditionals:
- the conditional must have a True value
- by using parentheses, rules of precedence can be ignored so that your operators are
used in the order that you wish
Using elseif
You can use as many elseifs as you wish as part of one if statement as long as else is the last part of the conditional.
The Switch Conditional
This can save time and clarify an if-elseif-else conditional which has become too big, bulky, elaborate. The switch has only one condition which is the value of its variable. It uses cases and works through each case looking for the one that satifies its condition. It needs a 'break' (placed between each case in the conditional)to stop it once it has found its condition. Without the break, it will go right to the closing curly brace even after it finds what it is looking for.
The For Loop
The use of the forloop is to execute sections of code repeatedly according to a number you designate.
for (initial expression; condition; closing expression){
statement(s);
}
Here is an example for causing numbers from 1 to 10 to be printed to the page:
for ($v = 1; $v <=10; $v++){
echo "$v";
}
The Form
Complete this form to register
