PHP Difference between array() and []
Asked Answered
T

5

190

I'm writing a PHP app and I want to make sure it will work with no errors.

The original code:

<?php
$data = array('name' => 'test',
              'id'   => 'theID');

echo form_input($data);
?>

Would the following work with no errors or is not recommended for some reason?

<?= form_input(['name' => 'test', 'id' => 'theID']); ?>

Are there any difference?

I've looked again the data about array() and the short array method with square brackets [] in PHP.net but I'm not sure.

And also, is the short php tag <?= ?> fine for echoing? Is there any version issue? (provided is enabled in php.ini)

Thinking answered 21/7, 2013 at 12:51 Comment(5)
Short array syntax was introduced in PHP 5.4, there is no difference and the old method will not be removed, so it's safe to use either. Short tags are usually frowned upon, I wouldn't use them.Substructure
Tks, any reference/reason on not using php short tags?Thinking
Although <?= ?> aren't actually considered shorttags, they aren't disabled with the standard shorttags afaik so they should be fine for simple echoes.Potman
See php.net/manual/en/ini.core.php#ini.short-open-tag.Azimuth
Interesting read on <?= ?> tags. According to one of the comments "Rasmus Lerdorf himself made that very commit" programmers.stackexchange.com/questions/151661/…Rinse
T
262

Following [] is supported in PHP >= 5.4:

['name' => 'test', 'id' => 'theID']

This is a short syntax only and in PHP < 5.4 it won't work.

Telluric answered 21/7, 2013 at 12:55 Comment(6)
It seems 5.3.24 supports this syntax too. (couldn't confirm directly)Cubicle
is thee any difference ?Gastrointestinal
@CooPer, No, unless you count the typing length.Telluric
I wanted a reference and found this- php.net/manual/en/language.types.array.php - "As of PHP 5.4 you can also use the short array syntax, which replaces array() with []."Luciusluck
I'm loving this new syntax by the way. I personally doubt it, but do you think they'll ever implement { } as a shorthand (object) syntax? But then again I suppose (object) [...] is just as easyHabanera
@Prof83, You may use $data = new stdClass();$data->someProp = 'someValue'; using PHP standard class and $obj = (object) ['foo'=>'bar', 'baz'=>'biz']; to convert an array (using explicit type casting) to an object (stdClass) but regarding the {}, it could be implemented in future but not sure tho :-)Telluric
A
42

As of 2022, it has been 10 years since the [] syntax was added. That is long enough to drop array() except in old legacy programs, in my opinion.

Astrobiology answered 25/6, 2019 at 13:20 Comment(5)
@TheAlpha well, even today, I was curious to know if there was performance differencesMelanous
Did you measure? I assume they are simply alternate syntax, which should not be measurable.Astrobiology
@Melanous For some reason I see higher memory_get_peak_usage(); in php 7.4.9 while using [ ] syntax.Detrusion
It is very difficult to measure memory usage in PHP, due to the many layers of caching and optimizations involved in memory usage. An accurate measurement requires either finding out how to disable all these optimizations (if that is possible) or else measuring both cases being compared once and discarding the result (which will show artificially high memory usage), then alternately measuring both cases several times and averaging the measurements for each case. The ONLY difference between [] and array() is syntactic, since [] is an abbreviation for array().Astrobiology
If the performance difference between array(..) and [..] is of concern, you should be using a different language... just sayingRequest
M
6

If you are using 5.3 or previous version then you can't use [] as an array as well as associative array. If you are using 5.4 or later version of PHP then you can use either array() or [] to create an array, associative array or even multidimensional array.

Mariken answered 11/8, 2018 at 6:55 Comment(0)
A
4

And regarding the <?= ?> part of the question: it is largely not frowned upon, at least not in 2019.

  1. A good technical breakdown: https://softwareengineering.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php
  2. A note in PSR-1: Files MUST use only <?php and <?= tags.
  3. TL;DR: There is no reason you cannot or should not use it.
Arvonio answered 17/10, 2019 at 4:53 Comment(0)
A
-28

Using php 7.2, for me it seems rather then [I am a an array] {I am an array seems to work}. Difference is between {} and []. My code

<p>
  <label for="post_category"> Cat 1 </label>
  <input type="checkbox" name="post_category{first}" value="cat1">
  <br />
  <label for="post_category{second}"> Cat 2 </label>
  <input type="checkbox" name="post_category" value="cat2">
</p>
Ajax answered 11/1, 2019 at 7:23 Comment(3)
Where is the PHP? You are writing HTML.Amongst
Also, curly braces will NOT work for the case that OP asked about. You cannot create an array with curly braces, only access the array elements (since v5.4 up to now - v7.3).Arvonio
@Mahad Ali, probably you should just delete this answer as it doesn't apply to the questionThinking

© 2022 - 2024 — McMap. All rights reserved.