Follow this lesson in Ullman Chapter 6. The scripts are located in the 06 directory.
See the pages register.html and handle_reg.php described on Ullman pages 114ff.
PHP Operators
Operator Usage Type +Addition Arithmetic –Subtraction Arithmetic *Multiplication Arithmetic /Division Arithmetic %Modulus (remainder after division) Arithmetic ++Increment (add 1) Arithmetic —Decrement (subtract 1) Arithmetic =Assignment Assignment ==Equality (for testing) Comparison !=Inequality Comparison <Less than Comparison >Greater than Comparison <=Less than or equal to Comparison >=Greater than or equal to Comparison !Negation; negates any test Logical AND
&&And Logical OR
||Or Logical
XOROr NOT Logical .Concatenation String .=Concatenate to (put on the end of) the current value Concatenation and
Assignment +=Add to the current value Concatenation and
Arithmetic -=Subtract from the current value Concatenation and
ArithmeticSee the PHP.net manual page on Operators at http://us2.php.net/manual/en/language.operators.php
if (condition) {
#do stuff
}or:
if (condition)
{
#do stuff
}
Is a variable empty?
empty()
# var is null, 0 or “”
# Returns TRUE or FALSEIs a variable not null?
isset()
# var is not null
# var can be 0 or “”
# Returns TRUE or FALSEIs a variable numeric?
is_numeric()
# var is numeric
# Returns TRUE or FALSEIs a variable an array?
is_array()
# var is an array
# Returns TRUE or FALSEIs a variable a string?
is_string()
# var is a string
# Returns TRUE or FALSEIs a variable an integer?
is_integer()
# var is an array
# Returns TRUE or FALSEIs a variable a real date?
checkdate()
# var is a truly possible date
# Returns TRUE or FALSE
if (condition) {
#do stuff
} else {
#do different stuff
}
if (condition) {
# do option a
} elif (condition) {
# do option b
} else {
# do some default option
}
switch ($variable) {
case “value1” :
option1_code();
break;
case “value2” :
option2_code();
break;
default :
default_code();
break;
}
for (starting_value, ending_value, increment) {
# do some stuff each time
}It may make more sense like this:
for ($x = 1, $x < 10, $x++) {
# do some stuff each time
print “\$x is now $x”;
}
foreach ($array as $key => $value) {
print “Key is $key. \n Value is $value.”;
}
while (this_condition_is_true) {
#do stuff
}A while loop can’t set initial conditions and can’t execute closing expressions.
A while loop may not execute even once if the initial condition is false!
do {
# stuff
} while (condition_is_true)
A do…while loop will always execute at least once.
Break! Continue! Exit! and Die!
Break: Exit the loop. Code that follows the loop executes.
Continue: Leave the current point in the loop. Loop continues to execute.
Exit: Stop executing all PHP code! – and don’t send following HTML code.
Die: Stop executing all PHP code! – and don’t send following HTML code.
Review Chapter 6 of Ullman.