Table of Contents

Web Development Lesson 7 - Databases

Named Arrays

Objective

In this activity you will learn some advanced array techniques for arrays in PHP, which will be useful when processing data from a database.

Setup

<?php
    include('header.php');
    include('menu.php');
?>
    <main>
    </main>
<?php
    include('footer.php');
?>

Named Arrays

$animals = array('dog', 'elephant', 'rabbit');
    $animals = array('item1' => 'dog', 'item2' => 'elephant', 'item3' => 'rabbit');

Displaying Arrays

    echo $animals;
    print_r($animals);
    $animals['item1'] = 'dog';
    $animals['item2'] = 'elephant';
    $animals['item3'] = 'rabbit';
    echo "MURRAY: the first item of animals is $animals['item1'].";

Foreach

    foreach($animals as $name => $item) {
        echo "MURRAY: $name is a $item";
    }

While Loop

    $i = 0;
    while ($i < 10) {
        error_log("MURRAY: the value of i is $i");
        $i++;
    }
cat /var/log/apache2/error.log | grep 'MURRAY'

Objects

$table->legs = 4;
$table->move($location);

Next: Tables