189

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)

Mr.Web
  • 6,992
  • 8
  • 51
  • 86
  • 2
    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. – JimL Jul 21 '13 at 12:54
  • 2
    Tks, any reference/reason on not using php short tags? – Mr.Web Jul 21 '13 at 12:57
  • 8
    Although = ?> aren't actually considered shorttags, they aren't disabled with the standard shorttags afaik so they should be fine for simple echoes. – Alexander Varwijk Jul 21 '13 at 12:58
  • 2
    See http://www.php.net/manual/en/ini.core.php#ini.short-open-tag. – str Jul 21 '13 at 13:23
  • 1
    Interesting read on = ?> tags. According to one of the comments "Rasmus Lerdorf himself made that very commit" http://programmers.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php – johnsnails Apr 16 '14 at 22:02

5 Answers5

261

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.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • It seems 5.3.24 supports this syntax too. (couldn't confirm directly) – Alberto Dec 08 '14 at 18:22
  • 4
    is thee any difference ? – Ali Akbar Azizi Dec 29 '14 at 14:25
  • 32
    @CooPer, No, unless you count the typing length. – The Alpha Dec 29 '14 at 14:35
  • 10
    I wanted a reference and found this- http://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 []." – mrwaim Feb 12 '15 at 12:30
  • 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 easy – Prof Jan 27 '16 at 13:08
  • 2
    @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 :-) – The Alpha Jan 27 '16 at 17:39
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.

Movahhedi
  • 302
  • 4
  • 15
David Spector
  • 1,520
  • 15
  • 21
  • 5
    @TheAlpha well, even today, I was curious to know if there was performance differences – Cid Oct 03 '19 at 08:40
  • Did you measure? I assume they are simply alternate syntax, which should not be measurable. – David Spector Oct 03 '19 at 12:42
  • @Cid For some reason I see higher `memory_get_peak_usage();` in php `7.4.9` while using `[ ]` syntax. – krylov123 Nov 04 '20 at 11:09
  • 2
    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(). – David Spector Nov 05 '20 at 14:06
  • 2
    If the performance difference between `array(..)` and `[..]` is of concern, you should be using a different language... just saying – David Welch Mar 16 '22 at 18:29
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.

codeepic
  • 3,723
  • 7
  • 36
  • 57
Md. A. Barik
  • 429
  • 5
  • 12
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.
Aydin4ik
  • 1,782
  • 1
  • 14
  • 19
-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>
ray
  • 5,454
  • 1
  • 18
  • 40
Mahad Ali
  • 69
  • 1
  • 8
  • 30
    Where is the PHP? You are writing HTML. – domdambrogia Mar 05 '19 at 22:44
  • 4
    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). – Aydin4ik Oct 17 '19 at 04:44
  • 1
    @Mahad Ali, probably you should just delete this answer as it doesn't apply to the question – Mr.Web Aug 12 '22 at 08:01