PHP: Associative Arrays II

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Associative Arrays II

Aims:

Built-in functions

Class exercise: what is the output of the following?

$month_lengths = array('Jan' => 31, 'Feb' => 28,
    'Mar' => 31, 'Apr' => 30, 'May' => 31, 'Jun' => 30, 'Jul' => 31, 
    'Aug' => 31, 'Sep' => 30, 'Oct' => 31, 'Nov' => 31, 'Dec' => 31);

if ( array_key_exists( 'Mar', $month_lengths ) )
{
    echo '<p>Beware the Ides of March</p>';
}
if ( isset($month_lengths['Mar']) )
{
    echo '<p>The Ides of March are come</p>';
}

if ( array_key_exists( 'April', $month_lengths ) )
{
    echo '<p>April is the cruelest month</p>';
}
if ( isset($month_lengths['April']) )
{
    echo '<p>Drip, drip, drop, little April showers</p>';
}

$months = array_keys($month_lengths);
foreach ($months as $month)
{
    echo "<p>{$month}</p>";
}

foreach (array_values($month_lengths) as $length)
{
    echo "<p>{$length}</p>";
}

Class exercise

$_GET is an associative array

PHP's superglobals

Class exercise