PHP Expressions and Control

This is material from Chapter 4 in the text. Again, I've skipped a bit.

  1. PHP Expressions.
    1. Expressions are made up of values and operators, under a specified grammer.
    2. Values are variables or constants.
    3. As in algebra, operators have precedence and associativity.
    4. Use parentheses to change the order of operation.
    5. Some usual things.
      1. Numbers: + - * / %
      2. Strings (concatentation): .
      3. Comparison: == != < > <= >= (and some others)
      4. Logical: ! && ||
      5. Your text has a much more complete list.
    6. PHP allows type casts with a notation like C and Java: (string)$val
    7. PHP has a type NULL, roughly like null in Java. All unititialized variables have the value NULL.
    8. PHP contains much truth.
      1. PHP just barely has a boolean type.
      2. Boolean constants are TRUE and FALSE, case insensitive, which are just 1 and NULL.
      3. Pretty much anything will automatically convert to boolean.
        1. The following values convert to FALSE:
          1. integer 0
          2. floating point 0.0
          3. the empty string
          4. the string "0" (containing the digit zero)
          5. an array with zero elements
          6. NULL
          7. One other obscure thing.
        2. All other values, including -1 and the strings "00", "0.0" and "FALSE", convert to true.
      4. If you print a boolean value, you will see 1 or nothing (empty string).
    9. Equality.
      1. The equality comparison == will convert values before comparison.
      2. The identitry comparison === will not. In particular, values of different type are never equal.
      3. For instance, 0 == "" is true, but 0 === "" is not.
      4. The inverted form of === is !==
    10. Logical operations.
      1. !, ||, &&
      2. NOT, OR, AND
      3. Do the same thing, but the spelled-out ones have lower precedence.
  2. Conditional
    1. Execute some code if a condition is true.
    2. Optionally, a different code when false.
    3. PHP also accepts elseif in place of else if.
    4. Bodies must be enclosed in { } if they contain more than one statement.
    5. Usually a good idea to enclose them regardless.
    [ PHP If Statement Source ]
  3. Looping.
    1. Loop bodies use brackets under same rules.
    2. While loop: repeat some code while a test is true
      1. Test first.
      2. If true run the body then test again.
      3. May run zero times if test is initially false.
      4. Test may become false anywhere within the body, but body will finish anyway.
    3. For loop: An extended while loop.
      1. Initialize a variable
      2. Run while the test is true.
      3. Incrementing each iteration.
    4. Do loop: A while loop, but must always run the first iteration.
    [ PHP Loops Source ]
  4. Foreach
    1. Repeat some code for each member of an array.
    2. Form for values, and another for key and value.
    [ PHP Foreach Statement Source ]
  5. Some examples.
    1. Grid: [ 5x7 Source ] [ 10x8 ]
    2. Dot row
      1. This one creates links that send a parameter back to itself.
      2. This creates an interactive page with a server turn-around.
      3. [ Row of colors Source ]