Codepath

PHP Conditionals

Online resources

Key concepts to understand

  • if(), elsif(), else()
    • Syntax
    • How conditional determines execution of code
<?php
  $alice = 25;
  $bobby = 20;
  if ($alice < $bobby) {
    echo "Alice has less than Bobby.";
  } elseif ($alice > $bobby) {
    echo "Alice has more than Bobby.";
  } else {
    echo "Alice has the same as Bobby.";
  }
?>
  • Comparison operators

    • ==
    • !=
    • <, <=, >, >=, <>
    • ===
  • Logical operators

    • &&
    • ||
    • !
  • switch()

    • Syntax
    • Why one should use "break" after each case
<?php
  switch($i) {
    case 0:
      echo "i equals 0";
      break;
    case 1:
      echo "i equals 1";
      break;
    case 2:
      echo "i equals 2";
      break;
    default:
      echo "i is not equal to 0, 1 or 2";
      break;
  }
?>
Fork me on GitHub