Replace list() by placing value in each index of an array

Asked

Viewed 312 times

3

So I was browsing php.net when I came across List() and went to see what it was for. Then they gave me the following example:

$info = array('Café', 'marrom', 'cafeína');
list($bebida, $cor, $substancia) = $info;
echo "$bebida é $cor e $substancia o faz especial.\n";

On that one I ask myself, Can’t I, instead of doing this, be like,:

$info = [$bebida, $cor, $substancia]; 
aí fazer tipo 
$info[0] = "café"
$info[1] = "marrom"
$info[2] = "cafeína" 

And then just call on one echo of life-type "$info[0] é $info[1] e $info[2] o faz especial. \n"??

2 answers

2

The command list offspring of a particular array a list of variables in a simple and unique operation, is a facilitator when programming, but, nothing prevents using each position of the array to print value, as you did in the example.

These are possible ways in developing with , is often useful in certain scenarios, as everything in system development.

This command can also be used to array simples or bidimensionais.

Simple array:

$info0 = array('um', 'dois');
list($a1,$a2) = $info0;
echo $a1; // um
echo $a2; // dois

Two-dimensional array:

$info1 = array(array('versao', 'forma'), array('razão', 'cultura'));
list($b0, $b1) = $info1;
var_dump($b0); //    
//array(2) {
//  [0]=>
//  string(6) "versao"
//  [1]=>
//  string(5) "forma"
//}
var_dump($b1); //
//array(2) {
//  [0]=>
//  string(6) "razão"
//  [1]=>
//  string(7) "cultura"
//}

that is, it draws from each position of a array for variáveis simple to facilitate the coding of your script.

It is always good to know the alternatives and often giving maintenance in third party code not be surprised by this type of coding.

References:

  • 1

    That’s exactly what I’m studying. I realized there are many ways to do various things in php. But I’m still starting and I’m trying to differentiate situations and know at what point I use such code. Thank you for the support!

2


The capacity of the function list goes far. There is the ability to handle arrays in more specific cases than your code will depend on. I consider that the list, can be used and abused, has the ability to make the code more semantic.

Your example would be more semantic if:

<?php
$frase = "café marrom cafeina";
list($bebida, $cor, $substancia) = explode(" ", $frase);

if ($cor == 'marrom') {
    $cor = 'preto';
}

echo sprintf("%s é %s e %s o faz especial.", $bebida, $cor, $substancia);

So let’s face it $info[1] the guy needs to get back up in the code, interpret the array and then, understand that the key 1 is actually a variable with the attribute cor. So that way the code loses the chance to be more understandable.

My example is not the best, but my intention with the answer is to tell you that not only because you have a more "common" means of doing things, this means is the best one to use. Research, study, investigate, mature your code.

  • 1

    Good example +1.

  • 1

    Aaaah, I always have a hard time understanding why things in php because I can’t have dimension of it being used on a social network, for example. Some big code. In the examples of 5 lines that I find on the internet, I have no notion of "why does it make things easier?". Thanks for answering, I will research more deeply on the list and see it applied in large projects. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.