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