Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm writing a PHP app with Codeigniter 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)

share|improve this question
    
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
1  
Tks, any reference/reason on not using php short tags? – Mr.Web Jul 21 '13 at 12:57
6  
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  
1  
Interesting read on <?= ?> tags. According to one of the comments "Rasmus Lerdorf himself made that very commit" programmers.stackexchange.com/questions/151661/… – johnsnails Apr 16 '14 at 22:02
up vote 88 down vote accepted

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.

share|improve this answer
1  
is thee any difference ? – CooPer Dec 29 '14 at 14:25
7  
@CooPer, No, unless you count the typing length. – The Alpha Dec 29 '14 at 14:35
4  
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 []." – mrwaim Feb 12 '15 at 12:30
4  
@Alberto, It is supported in PHP >= 5.4 Only. Check here : 3v4l.org/VeRbj – Vin Apr 21 '15 at 10:52
1  
@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 at 17:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.