Unlock the power of list: Practical usecase of list() in php.
Tags: php, laravel, cakephp, arrays
Published: 2023-08-06 14:00:00

Hi 👋, yet another practical usecase of the "native php function array",  `list()`. In this tutorial lets see an example for a likely everyday usecase for you. 👀 Let's go:

Wait 🙋! `List()` is not an array function but a [language construct](https://chat.openai.com/share/e56766cf-7c5e-4bac-8845-7bbe347afacf) More like reserved keywords that have specific meaning and are used to define the structure and behaviour of code. Reason why I had the quotes in my intro. lol

Enough. Lets get to business, shall we ?! 🤗

So what is a `List()` ?
`List()` is used to assign values to a list of variables at once or in a single operation.It provides a convenient way to extract values from an array and assign them to individual variables in one go. You will find `list()` to be used mostly with arrays.  Let's see an example:

```php
$countries  = ['Nigeria', 'Canada', 'Lithuania'];
// we want to extract the items in the array and assign them a variable
                                        // the array
list($africa, $northAmerica,  $europe) = $countries;

// Now the variables are assigned the values from the array
echo $africa; // Output: Nigeria
echo $northAmerica; // Output: Canada
echo $europe; // Output: Lithuania

```

Here is that something for you:

If your application makes use of images and you have a situation where you want to get the size of a valid image only file and at the same time return other information about this image like the dimensions, file type and a height/width in text strings,  `getimagesize()` can do this. Here is the typical result with this function:

```php
array(7) {
  800,           // Width of the image in pixels
  600        // Height of the image in pixels
  2,         // Type of the image (IMAGETYPE_JPEG)
  [3]=> string(23) "width="800" height="600"" // Image attributes for html
  "image/jpeg", // MIME type of the image
}
```
With `list()` you can conviniently assign variables to those items in the array and retrieve them in a more readable fashion.

Since we know that the first element of `getimagesize()` function is the image `width`, second, is the `height`, so and so forth... We can just represent them as variables using  `list()` :

```php
$arrayOfImageProperties = getimagesize("geeks.png"); 

list($width, $height, $type, $imageAttribute, $mime ) = $arrayOfImageProperties;

// display properties 
echo " The width of the image is  : " . $width . "<br>";
  
echo "The height of this  image is: " . $height . "<br>";
  
echo "The image type is:" . $type . "<br>";
  
echo "The image attribute of this image is:" .$attr;

```
Result:
```php
The width of this image is : 800
The height of this image is : 600
The image type is :2
The image attribute of this image :width="800" height="600"

```

#### Shorteeeer syntax:
Shorter syntax to save some key strokes 😉 :
From  php 7.1+ , you can do this: 

```php
 [$a, $b, $c ]= ['a','b', 'c'];
```
So incase you go through a codebase and see something like that, it's same as this:

```php
 list($a,$b, $c) = ['a','b', 'c'];
```

### The end.👋
I believe we can borrow some of these examples play around them and improve our code. See other [ examples here](https://www.php.net/manual/en/function.list.php#120463)