Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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
5  
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

1 Answer 1

up vote 59 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
2  
It is new to me, but a step in the good direction :) – Tomasz Kuter Jun 9 '14 at 22:28
    
All the best :-) – The Alpha Jun 9 '14 at 22:29
2  
@CooPer, No, unless you count the typing length. – The Alpha Dec 29 '14 at 14:35
2  
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 at 12:30
1  
@Alberto, It is supported in PHP >= 5.4 Only. Check here : 3v4l.org/VeRbj – Vin Apr 21 at 10:52

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.