I am Learn.i.ng

Some handy PHP functions

Posted:    Updated:   by Afolabi

One beautiful thing about PHP is how you can start building with basic knowledge then you refactor your codebases as you learn more. In this post, I will show how to use some functions that will reduce the number of lines you need to type to achieve your goals.

1. array_walk()

array_walk() provides a simple but neat way to perform operations on array elements without writing much. I wrote a detailed explanation of how to use it here.

2. list()

Imagine to you have an array of user data like so:

$person = ["John Doe", "Male", 42];

This is not an associative array but you can intuitively infer that it contains the person’s name, gender and age. You can access the values by referring to the numeric index of each element. However, wouldn’t your code be easier to read if you could assign those values to variables using a single line of code like so:

list($name, $gender, $age) = ["John Doe", "Male", 42];

This expected output would be:

echo $name;
John Doe
echo $age;
echo $gender;

Smooth, right?

Leave a Reply