Introduction to PHP for Os X by Example

Basic Control Structures

  • Comparison operators determine a true or false outcome in test expressions (==, !=, <, >, <=, >=).
  • The unary not operator, !, can negate any expression (makes true false and false true).
  • PHP (unlike Perl) has predefined Boolean values of true and false.
  • The same comparison operators are used to compare both numbers and strings.
  • String comparison is based on where the names would come when sorted in a list (Dictionary sort order).
  • The if / elseif / else control structure is used to check if a series of conditions are true and to execute a block of code if one, other or none are true.
  • Its best to check whether floating point numbers differ from one another by an acceptable threshold rather then using the equality operator.
  • A while loop is used to loop over a block of code while a condition remains true.
  • For short inline statements with a true/false outcome there’s a shorthand syntax: (test equation)? [true result]: [false result]
  • The for loop contains three expressions. The first is the initialization expression ($i) it is evaluated when the loop starts. The second is the test expression ($i>0) which is evaluated once each time through the loop. If its true the code block is executed if false the program moves on. The third is the iteration expression ($i–).

Resources