Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are arrays implicitly created in PHP when one of its keys are assigned something?

Just wishing to quickly verify this. It is different from my immediate experience from other languages whereby an array must first be declared before it can be filled with values.

like image 500
Hamster Avatar asked Jan 20 '23 03:01

Hamster


2 Answers

Yes, PHP will automatically create an array given any of the following

$foo[] = $bar;
$foo[1] = $bar;
$foo['bar'] = $bar;

// and of course
$foo = array();

// and soon to pass
$foo = [1, 2, 3];
like image 164
Phil Avatar answered Jan 22 '23 06:01

Phil


PHP will create the array even without being implicitly declared, yes.

$array[] = ...

$array would be a valid array.

like image 20
Blake Avatar answered Jan 22 '23 05:01

Blake