PHP I : Control Structures

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.

 

Operators

PHP Operators: Tests of Equality and so forth
+
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

XOR
Or 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
Arithmetic

See the PHP.net manual page on Operators at http://us2.php.net/manual/en/language.operators.php

 

Using the if conditional

if (condition) {
#do stuff
}

or:

if (condition)
{
#do stuff
}

 

Things you can test

Is a variable empty?

empty()
# var is null, 0 or “”
# Returns TRUE or FALSE

Is a variable not null?

isset()
# var is not null
# var can be 0 or “”
# Returns TRUE or FALSE

Is a variable numeric?

is_numeric()
# var is numeric
# Returns TRUE or FALSE

Is a variable an array?

is_array()
# var is an array
# Returns TRUE or FALSE

Is a variable a string?

is_string()
# var is a string
# Returns TRUE or FALSE

Is a variable an integer?

is_integer()
# var is an array
# Returns TRUE or FALSE

Is a variable a real date?

checkdate()
# var is a truly possible date
# Returns TRUE or FALSE

 

Using if/else

if (condition) {
#do stuff
} else {
#do different stuff
}

 

Using if/elif/else

if (condition) {
  # do option a
} elif (condition) {
  # do option b
} else {
  # do some default option
}

 

Using the Switch Conditional

switch ($variable) {
  case “value1” :
    option1_code();
    break;
  case “value2” :
    option2_code();
    break;
  default :
    default_code();
    break;
}

 

The for Loop

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”;
}

 

The foreach Loop

foreach ($array as $key => $value) {
   print “Key is $key. \n Value is $value.”;
}

 

The while Loop

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!

 

The do…while Loop

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.

 

Outside of class:

Review Chapter 6 of Ullman.