Difference between array() and [] in PHP
An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.
Syntax:
array( key => value, key2 => value2, key3 => value3, ... )
The comma after the last array element is not required can be omitted but this can help to understand other developers that the array is updated or not. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multiple line arrays, on the other hand, the trailing comma is commonly used, as it allows easier to add new elements at the end of the present array.
Note: Only the difference in using [] or array() is with the version of PHP you are using. In PHP 5.4 you can also use the short array syntax, which replaces array() with [].
Example:
<?php $array = array ( "geek" => "tech" , "tech" => "geek" , ); var_dump( $array ); // As of PHP 5.4 $array = [ "geek" => "tech" , "tech" => "geek" , ]; var_dump( $array ); ?> |
Output:
array(2) { ["geek"]=> string(4) "tech" ["tech"]=> string(4) "geek" } array(2) { ["geek"]=> string(4) "tech" ["tech"]=> string(4) "geek" }
Recommended Posts:
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- What is the difference between array_merge and array + array in PHP?
- Difference Between indexOf and findIndex function of array
- Difference between String and Character array in Java
- Difference between ASP and ASP.NET
- Difference between “!==” and “==!” in PHP
- Difference between JSP and ASP
- What is the difference between GUI and CUI?
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- What is the difference between CSS and SCSS ?
- Difference Between HTML and ASP
- Difference between "." and "#" selector in CSS
- Difference between JavaScript and Php
- Difference between Where and Group By
- What is the difference between the | and || or operator in php?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.