PHP- [] and array() while declaring a variable [duplicate] - php

This question already has answers here:
PHP array vs [ ] in method and variable declaration
(5 answers)
Closed 1 year ago.
I want to know if there is a difference, and what is the difference between declaring a variable as [] and as array()
i.e
Difference between $p=[] and $p=array()

The difference being the array() is for versions 5.4 and below, whereas [] can be used in versions 5.4 and above.
The [] array is quicker than using array() as well.

Related

What's the difference between these two array creation methods? [duplicate]

This question already has answers here:
PHP Difference between array() and []
(5 answers)
Closed 9 months ago.
I want to know if there's any difference in either the output or the object being created when these commands are run to create the object $array.
$array = ["Sagittarius", "Cancer", "Gemini", "Pisces"];
$array = array("Sagittarius", "Cancer", "Gemini", "Pisces");
Thanks for the feedback!!
They are the same but [] is the shortened version introduced in PHP 5.4.0.
http://docs.php.net/manual/en/language.types.array.php

Differences in this Array Declarations in PHP [duplicate]

This question already has answers here:
PHP array vs [ ] in method and variable declaration
(5 answers)
PHP: What's the difference between initializing an array with "new" vs without it?
(4 answers)
Closed 1 year ago.
What is the difference between declaring:
$myArray = Array();
and:
$myArray = [];
There's no difference, it's just two alternative syntaxes. See https://www.php.net/manual/en/language.types.array.php

making array with array() or []? - PHP [duplicate]

This question already has answers here:
Best way to initialize (empty) array in PHP
(9 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
PHP Difference between array() and []
(5 answers)
Closed 3 years ago.
in evey tutorial they said you should make an array with "array()" but I realize that I can also make it with "[]" what are the difference?
array() :
$a = array("red","blue","green");
[] :
$a = ["red","blue","green"];

Unexpected '[' error [duplicate]

This question already has answers here:
PHP Difference between array() and []
(5 answers)
Closed 8 years ago.
I have an Index php page that contains a code that prints the values of an array $a=['f','a','b'];.
The code works fine on WAMP server on my computer however when I upload the page online on a server like 000webhost an error comes up that says there is unexpected '[' on line of the array. Does anyone know why this happens?
Your PHP version doesn't support the [] notation, this kind of notation is available from PHP 5.4, use $a = array('f','a','b'); instead.

What is this operator "=>"? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
Reference - What does this symbol mean in PHP?
I see this symbol in a lot of PHP code.
I can't figure out what it means or what it does..
Is it really an operator ?
Do you mean =>? If so, it's for initializing array keys (or indexes if you prefer). Like this:
$values = array(
'foo' => 'bar'
);
This will initiazlie an array with a key named foo with a value of bar.
Read more on about arrays at php.net.
http://php.net/manual/en/language.operators.php

Categories