Lesson 6

Working with Complex Data

You have learned how to work with variables that store text and numbers. This works well for simple data, but what if you have a list you need to manipulate or you want to know how many days there are between January 15, 2011, and March 2, 2011?

In this lesson you learn how to use special variables that are designed for specific types of data.

Working with Arrays

An array holds multiple values in a single variable. Within one variable you have an entire list of values. You refer to and access the entire array just by the array name as you would a regular variable, or you can use indexes to access the individual values. You can even nest arrays within arrays. These nested arrays are called multi-dimensional arrays.

Let's display a list of employees on a web page. You could assign each employee to a regular variable and then display each of those variables. Your results look similar to Figure 6-1.

<?php
// Assign Value
$employee1 = ‘Sally Meyers’;
$employee2 = ‘George Smith’;
$employee3 = ‘Peter Hengel’;
?>
<html>
<head>
  <title>Lesson 6a</title>
</head>

<body>

<h1>Employee List</h1>
  <p><?php echo $employee1; ?></p>
  <p><?php echo $employee2; ?></p>
  <p><?php echo $employee3; ?></p>

</body>
</html>

Now try it using an array. There are two types of arrays in PHP. The first type is a numeric array where the index is the position of the ...

Get PHP and MySQL® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.