63

How may I initialize multiple PHP variables with a value of zero simultaneously without using an array? I wish to write code that is essentially equivalent to the following:

$first = 0;
$second = 0;
$third = 0;
$fourth = 0;
slevy1
  • 3,797
  • 2
  • 27
  • 33
dreamer_999
  • 1,465
  • 3
  • 17
  • 22
  • Possible duplicate of [Assign same value to multiple variables](http://stackoverflow.com/questions/11651594/assign-same-value-to-multiple-variables) – Anderson Green Aug 27 '16 at 00:56

3 Answers3

132
$first = $second = $third = $fourth = 0;
rid
  • 61,078
  • 31
  • 152
  • 193
  • Doesn't this run into the problem that when you change $fourth, you also change the rest? – Alec Jul 28 '16 at 08:09
  • 11
    If you change `$fourth`, you won't change the rest, because `0` is a value, not a reference. If you would have done something like `$fourth = 0; $third = &$fourth; $second = &$fourth; $first = &$fourth`, then all the other variables would be references to `$fourth` (not the value `0`), and changing `$fourth` would be reflected in the other variables as well. – rid Jul 28 '16 at 17:14
  • 14
    This multiple assignment works because, in PHP, assignment is an expression which returns the assigned value. Thus, `$fourth = 0` is an expression which returns the value `0`. This value is then assigned to `$third`, and, since this assignment is also an expression, the return value of assigning `0` to `$third` is again the value `0`, etc. – rid Jul 28 '16 at 17:17
22

While it is feasible to initialize multiple variables using a comma operator within a for-loop, as follows:

<?php
for ($a=0,$b=0,$c=0,$d=0;;) {
    break;
}
var_dump($a,$b,$c,$d);

(See demo here)

the list construct provides a more efficient way to perform multiple variable assignment, as depicted in the following example:

<?php

list( $first, $second, $third, $fourth ) = array( 0, 0, 0, 0 );
var_dump($first, $second, $third, $fourth );

See demo here

One may wish to reconsider avoiding the usage of arrays to achieve multiple initialized variables. With PHP7.1+ one may write simpler, robust code if one utilizes array destructuring available with short array syntax, as follows:

<?php

[$first, $second, $third, $fourth ] = [0, 0, 0, 0];
var_dump($first, $second, $third, $fourth );

See demo here.

If one needs to be certain that the variables being initialized were not previously set, see this related discussion, particularly this response.

slevy1
  • 3,797
  • 2
  • 27
  • 33
2

If you want to initialize multiple array variables then use

# Initialize multiple array variables with Empty values
$array_1 = $array_2 = $array_3 = array();

# Initialize multiple array variables with Some values in it
list( $array_1, $array_2, $array_3) = array('one','two','three');

# Print value of array variables
var_dump($array_1,$array_2,$array_3);

Output:
*******
string 'one' (length=3)
string 'two' (length=3)
string 'three' (length=5)

If you want to initialize multiple regular variables then use

# Initialize multiple regular variables with values
$a = $b = $c = 'Hello PHP';
echo $a.'<br>',$b.'<br>', $c.'<br>';

Output:
*******
Hello PHP
Hello PHP
Hello PHP
Yogi Ghorecha
  • 1,584
  • 1
  • 19
  • 26