DEV Community

Chidiebere Chukwudi
Chidiebere Chukwudi

Posted on • Updated on • Originally published at jovialcore.tech

Unlock the power of list: Practical usecase of list() in php.

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 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:

$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

Enter fullscreen mode Exit fullscreen mode

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:

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
}
Enter fullscreen mode Exit fullscreen mode

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() :

$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;

Enter fullscreen mode Exit fullscreen mode

Result:

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"

Enter fullscreen mode Exit fullscreen mode

Shorteeeer syntax:

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

 [$a, $b, $c ]= ['a','b', 'c'];
Enter fullscreen mode Exit fullscreen mode

So incase you go through a codebase and see something like that, it's same as this:

 list($a,$b, $c) = ['a','b', 'c'];
Enter fullscreen mode Exit fullscreen mode

The end.👋

I believe we can borrow some of these examples play around them and improve our code. See other examples here

Top comments (0)