WBDV243

Web Authoring II

PHP

Control Structures

Making Decisions

  • A condition must either be true or false
  • True and false are boolean values
  • Decisions can be based on comparison of two non-boolean values
  • PHP booleans can be explicit or implicit

Explicit Boolean Values

  • Keywords: true and false
  • Case-sensative
    • $OK = true;
    • $OK = FalSe;
  • Do NOT enclose in quotes

Implicit Boolean Values

PHP considers all of the following false

  • The keywords false and null
  • Zero as a number or string (0, 0.0, '0')
  • An empty string ('', "")
  • An empty array

Conditional Statements


<?php
if (condition is true) {
  do something
}

<?php
if (condition is true) {
  do something
} else {
  do something else
}

<?php
if (condition is true) {
  do something
} elseif {
  perform alternative task
} else {
  do something different
}

Comparison Operators

Symbol Meaning
== Both values are equal
!= Values are not equal
=== Values are identical
!== Values are not identical
> Value on the left is greater than the one on the right
>= Value on the left is greater than or equal to the one on the right
< Value on the right is greater than the one on the left
<= Value on the right is greater than or equal to the one on the left

Let's Do An Example!

Switch


<?php
switch ($var) {
  case 'value1':
    // do something
    break;
  case 'value2':
    // do something different
    break;
  default:
    // do something else
}

Loops

While Loops


<?php
while (condition) {
  // code to be executed
}

<?php
// initialize value
$i = 1;

while ($i < 10) {
  echo $i . '
'; // increment $i $i++; }

Do … While Loop


<?php
do {
  // code to be executed
} while (condition)

<?php
// initialize value
$i = 100;

do {
  echo $i . '
'; // increment $i $i++; } while ($i <= 10;)

For Loop


<?php
for (initialize; condition; increment) {
  // code to be executed
}

<?php
$characters = ['Bugs','Daffy','Speedy'];

for ($i = 0; $i < count($characters); $i++) {
  echo $characters[$i] . '
'; }

Let's build a list of characters in HTML!

Accessing Arrays - foreach


<?php
foreach ($array_name as $value) {
  // do something with $value
}

<?php
$characters = ['Bugs','Daffy','Speedy'];

foreach ($characters as $item) {
  echo $item . '
'; }

Accessing Array Keys and Values


<?php
foreach ($array_name as $key => $value) {
  // do something with $key and $value
}

<?php
$descriptions = [
  'Bugs' => 'bunny',
  'Daffy' => 'duck',
  'Speedy' => 'the fastest mouse'
];

foreach ($descriptions as $key => $description) {
  echo "

$key is $description

"; }

Try It!


<?php
foreach ($descriptions as $key => $description) {
  echo "

$key is"; if ($key !== 'Speedy') echo ' a'; echo "" $description

"; }

Functions

PHP has many built in functions.

  • phpVersion();
  • strtolower();
  • sort();

User Defined Functions


<?php
function f_name($parameters) {
  // do something
}

<?php
function double_value($my_number) {
  return $my_number * 2;
}

$i = 2;
echo double_value($i); // 4

Includes


<?php
include 'some_folder/some_file.php';

<?php
include 'includes/functions.php';

Let's Get to Work