With every release of PHP new version, new functionality,improvement and bug fixes are released. PHP 5.4 introduces the concept of Generators and traits etc. Apart from new functionality, PHP 5.4 introduces many improvements one such improvement is shorter syntax of array declaration.
Till PHP 5.3 an array can be created using array() language construct.
1 2 3 4 5 6 |
array( key1 => value1, key2 => value2, key3 => value3, ... ) |
1 2 3 |
// Old way of declaring sequential array $productparam = array('product_id', 'description', 'price'); // "old" way |
1 2 3 4 5 6 7 8 |
// Old way of declaring an Associative Array $fruits = array( "fruit1"=>"Apples", "vegetable" => "Tomato", "fruit2" => "oranges" ); |
Top Five Most Used PHP Array Functions
Array Declaration in PHP 5.4
In PHP 5.4, same can be written as
1 |
$productparam = ['product_id', 'description', 'price']; |
Javascript uses the same syntax for array declaration.
1 2 3 4 5 6 7 |
// Associative Array $fruits = [ "fruit1"=>"Apples", "vegetable" => "Tomato", "fruit2" => "oranges" ]; |
You can still use the old method of array declaration in new version of PHP. It’s an alternate syntax of declaring an array.