Learn Web Development – The Super Simple Way

Getting data

When you learn web development, you need to understand that you’re dealing with functionality and data. The data can come from anywhere. It can come from a database, a csv file, a json encoded string from an API, as long as it’s formatted properly for PHP to understand. Let’s take the following example.

Arrays

An array is simply a list of similar items; a grouping of data that represents information packaged neatly for quick reference. Let’s say we have a list / array of tasks. In PHP, a list of tasks looks like this:

$tasks = array("pick up kid from school", "drop off laundry", "go to the gym");

This sets our tasks array to hold three values. You can tell what they are by their string values. But this is inefficient for coding. So, we have to create a sort of an index for these values that PHP can reference. Let’s add our own indexing to our array. You can continue to write in our file, or erase everything and start fresh. Let’s keep things going, shall we?

$tasks = array(
    0 => "pick up kid from school",
    1 => "drop off laundry",
    2 => "go to the gym",
);

Notice how using the => notation, we were able to create our array with indexing. Task 1 is pick up kid from school and so on.

The previous declared array has this indexing automatically, but if you want to have different indexes, you would have to declare your own, i.e. 2, 255, 17

If we were to display this in our PHP, we could create a loop. Let’s loop through our task list and print out the text value. Write the following after our array declaration.

foreach ($tasks as $task) {
    echo $task . '<br />';
}

Let’s try to understand what each line means.

the foreach function is an internal, reserved PHP function that takes an array or object (sort of an array) and assigns the value of each array item to the $task variable. There’s a different version of this function that includes our key. Here’s how it works:

foreach ($tasks as $key => $task) {
    echo $key . " is the index for " . $task . '<br />';
}

When you save / refresh, you’ll notice that we’re printing out the index of the array item, as well as the actual array item value.

To learn web development, you need to learn about databases.