Summary of “Arrays and functions in PHP”

Functions

function is a fragment of code that is written once and then used as many times as required.

Before using a function, you need to declare it using the function keyword. Indicate the function name after it. It must begin with a Latin character or underscore.

Parentheses are written after the function name, and then the function body is written in curly braces. The function body contains the code that the function will execute.

function my_func() {
  muffin_log('I am the my_func function');
}

When a function is declared, PHP will remember it, but the code inside the curly braces is not executed. You need to call the function in order to execute it.

Write the function name and a set of parentheses in order to call the function.

my_func(); // Outputs: "I am the my_func function"

The function can be called any number of times.

The variables that are declared inside the function are available only from within this function. And external variables, on the contrary, cannot be seen inside the function. This is called the scope. You can read more about it in the documentation.

We can call one function inside another.

Function arguments

Arguments allow you to pass different kinds of data to functions and to influence what it outputs. Argument are indicated in parentheses when declaring a function. You can include as many arguments as you would like. If there are several arguments, they are separated by commas.

Arguments are similar to variables. They also store values, and their names must begin with a dollar sign. Arguments can only be used in the body of a function.

function my_func($number_1, $number_2) {
  muffin_log($number_1 - $number_2);
}

Argument values are specified in parentheses when the function is called. They are listed in a comma-separated list in the same order as their respective arguments when declaring a function.

my_func(3, 1); // Outputs: "2"
my_func(1, 3); // Outputs: "-2"

Functions Return Values

Functions are not only able to accept data, but they can also return it. In order to indicate what value a function should return, the keyword return should be used:

function my_func($number_1, $number_2) {
  return $number_1 - $number_2;
}

When a function is called, the value that it returns will take the place of the function in the code.

<!-- Display the function in the template -->
<p><?= my_func(3, 1) ?></p>

<!-- Result  -->
<p>2</p>

The function will finish executing after it returns the value. PHP will ignore the subsequent code in the body of the function.

function my_func($number_1, $number_2) {
  return $number_1 - $number_2;

  // The code on the next line will never be executed
  muffin_log('Is there life after a return?');
}

The function may return nothing or just one of the following values: a number, line, array, etc.

Built-in Functions

Built-in functions are the functions that were created by the creators of PHP. They form part of the language, and for that reason they do not need to be declared before you can use them. PHP contains thousands of predefined functions. There is no need to memorize them. After all, you can always look them up in the documentation.

We call a function that is passed as an argument to another function a callback. Callbacks are not built-in functions, since they are created by developers themselves.

uasort

The uasort is one of the built-in functions for sorting an array. This function accepts two arguments: the array that needs to be sorted and the name of the callback function.

uasort($array, 'callback');

The callback function that we pass to uasort has to take two arguments and then return a number: a positive number if the first argument is larger, a negative number if the second argument is greater, or zero if the arguments are equal in value.

When uasort is called, it passes two array elements to the callback, and it is able to determine which one is larger. It then sorts the array in ascending order from smallest to largest element.

array_slice

The array_slice function accepts an array, copies part of it, and returns it as a new array. The array that is passed as an argument to the function (the source array) is unchanged.

The function accepts four arguments:

  • The source array,
  • The element serial number from which the copying starts,
  • The number of elements that need to be copied.
  • The boolean value that determines whether the source array keys need to be saved into the new array.
$new_array = array_slice($array, $first_element, $length, $preserve_keys);

array_filter

PHP has a built-in filtering function: array_filter. It takes two arguments (the original array and the callback function) and returns a new array. The source array does not change.

$filtered_array = array_filter($array, 'callback');

According to the documentation, the callback must accept a source array element and return a boolean value of: true if this element should be included in the new array or false if this is not necessary.

array_rand

Use the array_rand function to obtain random elements from the array. It takes the source array and the number of random elements that we want to receive.

$random = array_rand($array, $number_of_elements);

If the second argument is one, then the function will return one random key. If the second argument is greater than one, then the function will return an array with random keys.

The array_rand function is responsible for choosing random elements, but it returns them in the order in which they have been entered in the source array.

shuffle

The shuffle built-in function shuffles the array. The function takes an array and arranges its elements in random order. Be careful, since the shuffle function changes the array that is transmitted to it and does not save the keys. It is best not to use it with associative arrays.

shuffle($array);

Combining Arrays

In PHP you can combine arrays using a plus sign.

$big_array = $array_1 + $array_2;

As a result we get a single array that includes elements from both the first as well as the second arrays. The order of the elements and their keys are saved.

If several elements have the same keys, then the value will be taken from the array that comes first. Before you combine them, make sure that the keys in the arrays are not the same or that the identical values are not assigned to identical keys.


Continue