Open In App

Difference between array() and [] in PHP

Last Updated : 17 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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"
}

Similar Reads

What is the difference between array_merge and array + array in PHP?
In PHP arrays can be joined using array union (+) operator or by using array_merge() function. There is a subtle differences in these two methods. The array_merge() function is an inbuilt function for joining two array regardless of their type (numerical, categorical etc.)array_merge() Function: The array_merge() merges one or more arrays provided
4 min read
Difference and Similarities between PHP and C
PHP is a server-side scripting language designed specifically for web development. It can be easily embedded in HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP from a client-side language like HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. C is a
3 min read
Difference between JavaScript and PHP
In this article, we will know about Javascript &amp; PHP, along with understanding their significant differences. A long time before, most people used to think PHP is a server-side language and Javascript as client-side language as it was only executed in web browsers. But after V8, Node and other frameworks came, Javascript is capable of doing a l
4 min read
Difference between try-catch and if-else statements in PHP
The try and catch are used in PHP for handling exceptions like other languages such as C++, Java, etc. An exception is unexpected result or unexpected state of a program that can be handled by the program itself. To handle this kind of unexpected results in PHP, try and catch are used. For more details, visit Exception Handling in PHP. Similarly, P
5 min read
Difference between isset() and array_key_exists() Function in PHP
isset() function The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. Syntax: bool isset( $var, mixed ) Parameters: This function a
2 min read
Difference between bindParam and bindValue in PHP
PDOStatement::bindParam() Function The PDOStatement::bindParam() function is an inbuilt function in PHP that is used to bind a parameter to the specified variable name. This function bound the variables, pass their value as input, and receives the output value, if any, of their associated parameter marker.Syntax: bool PDOStatement::bindParam ( $par
3 min read
What is the difference between the | and || or operator in php?
'|' Operator It is a bitwise OR operator. This operator is used to set the bits of operands if either a or b or both are set. It means the value of the bit will be set to 1. A B A | B 0 0 0 0 1 1 1 0 1 1 1 1 Syntax: $a | $b Program: &lt;?php $a = 3; $b = 10; echo $a | $b; ?&gt; Output: 11 Explanation: In above example, given two values, a = 3 and b
2 min read
What is the difference between public, private, and protected in PHP?
Public, private and protected are called access modifiers. Just like C++, PHP also have three access modifiers such as public, private and protected. The visibility of a property, a method or a constant can be defined by prefixing the declaration with these keywords. If the class member declared as public then it can be accessed everywhere.If the c
3 min read
Difference between require-dev and require in PHP?
Before going to understand the difference between require and require_dev first understand what is require and require_dev. require: These are must packages for the code to run. It defines the actual dependency as well as package version. require_dev: It defines the packages necessary for developing the project and not needed in production environm
3 min read
What is the difference between HTTP_HOST and SERVER_NAME in PHP?
HTTP_HOST: It is fetched from HTTP request header obtained from the client request Example: Website: https://www.geeksforgeeks.org HTTP_HOST: www.geeksforgeeks.org HTTP_SERVER: It is fetched from the server name based on the host configuration. Example: Website: https://www.geeksforgeeks.org HTTP_SERVER: Display the server name HTTP_HOST SERVER_NAM
2 min read