76

I want to create 5 variables of type array all at once. Is this possible? In Java I know you can, but can't find anything about PHP. I'd like to do something like this:

$var1, $var2, $var3, $var4, $var5 = array();
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
transformerTroy
  • 1,296
  • 2
  • 11
  • 12

5 Answers5

130

Yes, you can.

$a = $b = $c = $d = array();
Grim...
  • 16,518
  • 7
  • 45
  • 61
  • 19
    FWIW, in any language *except* PHP, the result would be multiple variables referring to the *same* array. That is, altering the array in one variable would also alter the array in the other variables. In PHP, because an array assignment does "copy by value" (unless the "reference operator" is used), this does what OP asks - it creates 4 array values, one per variable. [I mention this, so anyone who works in multiple languages will think carefully about what is happening, especially if they use this syntax in different languages.] – ToolmakerSteve Aug 07 '19 at 20:21
  • this answer is wrong. The result here is: All these vars gets the same value (that equals to `array()`). Th e solution is using of the `list()`. S. [this answer](https://stackoverflow.com/a/50984039/2019043). – automatix Jun 28 '23 at 12:16
42

Since PHP 7.1 you can use square bracket syntax:

[$var1, $var2, $var3, $var4, $var5] = array(1, 2, 3, 4, 5);

[1] https://wiki.php.net/rfc/short_list_syntax
[2] https://secure.php.net/manual/de/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring

d4rwel
  • 715
  • 5
  • 8
  • 11
    NOTE: This useful technique produces a different result than the accepted answer. The accepted answer puts the entire array into each variable. *This* answer puts first element into first variable, second into second, etc. So `$var1 = 1; $var2 = 2; ...` Especially useful for returning multiple values from a function - put the return values into an array, and extract them as shown. – ToolmakerSteve Aug 07 '19 at 20:03
  • So to initialize each of the left-side variables to an empty array, it would look like: [$var1, $var2, $var3] = [ [], [], [] ] – scott8035 Jan 09 '22 at 17:49
  • Although useful, it doesn't look as if the OP wanted to destruct an array, simply assign the same value to multiple vars at once. – digout Apr 22 '22 at 15:28
  • 1
    Upvote because it's the answer I was looking for when I asked the question in the title. It's not the right answer to the OP, but maybe it's the right place to put the answer to the question which it _is_ the right answer to! – Adam Chalcraft Nov 05 '22 at 23:57
22
$c = $b = $a;

is equivalent to

$b = $a;
$c = $b;

therefore:

$var1 = $var2 = $var3 = $var4=  $var5 = array();
Miquel
  • 4,741
  • 3
  • 20
  • 19
  • 9
    Your answer is confusing The equivalent should be: $b = $a; $c = $a; – valiD May 01 '16 at 22:34
  • `$x = [1,2]; $y = [5,6];` `$x[] = $y[] = 0;` according to your answer $x will become _[1,2,[5,6,0]]_ the actual result is **[1,2,0]** ie, `$c = $a;` – Anees Sadeek Nov 16 '16 at 10:11
  • 2
    The example just above is not using assignment as is stated in the original example but an array push. The return value for the array push is the value being pushed, not the array being pushed into. When you do an assignment, the return value from the assignment is the assigned variable. Or to put it another way, an array push returns the right side, an assignment returns the left side (which equals the right anyway). So in your example, the right array push returns the zero being pushed to the left assignment. You can see that the final value of `$y` also has the `0` pushed on. – DjB Oct 20 '17 at 03:20
  • 2
    @valiD I guess you are wrong in this case. PHP sets $b equals $a and $c equals $b. It passes the value step by step. So Miquel is right with his equivalent part. `$c = 1 + $b = $a = 1;` Makes $c equal 2 and not 1. – Dwza Oct 01 '19 at 14:12
12

I prefer to use the list function for this task. This is not really a function but a language construct, used to assign a list of variables in one operation.

list( $var1, $var2, $var3 ) = array('coffee', 'brown', 'caffeine');

For more information see the documentation.

Vernon Grant
  • 129
  • 1
  • 6
  • 2
    As with the newer square bracket notation [mentioned above](https://stackoverflow.com/a/53081424/2129574), this does not answer the OP question. Instead of copying the same value to all variables, this parses the individual values of the array. – Tim Morton Dec 15 '20 at 17:57
10
$var1 = $var2 = $var3 = $var4=  $var5 = array();
Francois
  • 10,730
  • 7
  • 47
  • 80
  • That's wrong. The result here is: All these vars gets the same value (that equals to `array()`). Th e solution is using of the `list()`. S. [this answer](https://stackoverflow.com/a/50984039/2019043). – automatix Jun 28 '23 at 12:15