The list() function [PHP]

The list() function was one of those functions that I didn't learn about in any PHP course, but has stumbled across while looking at others code. It's not really a function, but is a language construct but can be very handy.

Basically, it unpacks arrays and assigns the keys individual variables.

Here's an example of its use:

$mealsForToday = ['eggs and toast', 'fish and chips', 'steak and potatoes'];

list($breakfast, $lunch, $dinner) = $mealsForToday;

echo $breakfast;    // returns 'eggs and toast'
echo $lunch;        // returns 'fish and chips'
echo $dinner;       // returns 'steak and potatoes'

Of course, you could accomplish this same thing by setting each variable on it's own line, but to me, this is more elegant and easily understood, and that's a win.

To read more about the list() function, check it out in the PHP documentation.