Intermediate PHP 2 – Loops and Fuctions

Loops

In programming, loops are a helpful way to iterate through each item in an array. If we wanted to create a normal array with the names of each user, we loop through the array.

$names = [];
foreach ($users as $user) {
    array_push($names, $user['name']);
}

Code Dissection

$names = [];

This an empty array, but it won’t be empty for long.

foreach ($users as $user) {

foreach

The word foreach tells PHP we are about to loop through something.

($users

The first variable is our array of users.

as $user)

The second variable after the “as” word is each item in the array.

{}

The code inside of the curly braces is run for each item in the array. The first time, the $user variable will be the first associative array in the array ([“name” => “John”]). The next time, it will be the second item and so on.

array_push($names, $user['name']);

array_push

In PHP, we can use the array_push function to add an item to the end of our array. We’ll look at functions right after this.

$names

The first parameter of the array_push function is our empty array. This is the array that we want to add something to.

$user[‘name’]

The second parameter is what we want to add to the array. Since this is the first item in the loop, the name will be “John”. The second time around, it will be “Mary”.

If we look at the $names array, we’ll see that it’s a simple array of the user’s names.

[“John”, “Mary”]

Functions

Functions are a way to reuse code. They’re also a great way to let other programmers (and future you) know what a block of code is doing.

With that in mind, let’s create a function that creates an array of names.

function createNamesArray($humanUsers)
{
    $names = [];
    foreach ($humanUsers as $user) {
        array_push($names, $humanUsers['name']);
    }
    return $names;
}
$names = createNamesArray($users);

Code Dissection

function createNamesArray($humanUsers)

function

The “function” keyword tells PHP that we’re about to create a function.

createNamesArray

The text after the function keyword is whatever you want to call the function. The name should describe what the function does. Naming functions and variables is one of the trickiest parts of web development.

($humanUsers)

The function takes one parameter, an array of human users.

Most of the time, functions need some type of data to work on, and that data gets passed into the function via the parameter. Functions can have multiple parameters, but in this case, there is only one.

{}

Everything inside of the curly braces belongs to the function.

$names = [];
    foreach ($humanUsers as $user) {
    array_push($names, $smartUser['name']);
}

You probably recognize this block of code from before. It’s creating an array of names from an array of users. The biggest difference is that the array of users comes from the function’s parameter.

return $names;

At the end of the function, we return the array of names.

$names = createNamesArray($users);

$names =

The names variable will be whatever the function returns. If the function didn’t return anything, the names variable would be null.

createNamesArray

To call a function, write out the name of the function and parenthesis.

($users)

In the parenthesis, list any parameters. When calling a function, you pass any data into the function by passing it in as a parameter. The variable in the parenthesis is the parameter we are passing to the function. The variable is our list of users. That’s how most functions work in a nutshell. Give them some data (our array of users) and get something back (the array of names).