6

whats the meaning of this line

<input type=text name="name" value="<?= $name ?>

if we are to declare as PHP shouldn't we write <?php instead of <?=

Thanks

miken32
  • 35,483
  • 13
  • 81
  • 108
Josh
  • 1,559
  • 3
  • 11
  • 14

3 Answers3

15

<?= are PHP short open tags, which can be enabled (or disabled) via the short_open_tag directive in php.ini (quoting) :

This directive also affects the shorthand <?= , which is identical to <? echo . Use of this shortcut requires short_open_tag to be on.

And:

Also if disabled, you must use the long form of the PHP open tag (<?php ?> ).

This means your portion of code :

<input type=text name="name" value="<?= $name ?>

Is equivalent to this one :

<input type=text name="name" value="<?php echo $name; ?>

But only when short open tags are enabled.

And, as a sidenote : short open tags are not always enabled -- in fact, they are disabled by default, with recent versions of PHP.

Which means it might be wise to not depend on those, at least if you want to deploy your application on servers on which you are not administrator.

Amal Murali
  • 70,371
  • 17
  • 120
  • 139
Pascal MARTIN
  • 374,560
  • 73
  • 631
  • 650
  • I agree, relying on short tags is asking for a broken heart when you deploy on a server that is configured to not allow them :) – JC. Dec 24 '09 at 21:20
  • In defense of short tags, they're invaluable if you use PHP as its own templating layer. Additionally, it can be set PHP_INI_PERDIR -- i.e., in .htaccess on a per-app basis. – Frank Farmer Dec 24 '09 at 21:58
  • 1
    "=" won't be deprecated on PHP6 and since PHP5.3 "is now always available, regardless of the short_open_tag php.ini option." – Carlos C Soto Feb 13 '14 at 21:10
  • 1
    I know this is an old answer but it may be useful to others if you updated it to reflect the current documentation. In particular, the fact that [since 5.4.0, `=` is enabled, even without short tags](http://www.php.net/manual/en/ini.core.php#ini.short-open-tag). – Tom Fenech Apr 20 '14 at 10:38
  • Why if I add a function at the variable inside the shortcourt tag(like: ` addslashes($string); ?>` ) didn't print anything? It's an error adding the `echo` function using the shortcourt (` echo addslashes($string); ?>`), or is better use it in normal `` tags? – Andrea_86 Oct 22 '14 at 10:32
10

<?= ... ?> is shorthand for <?php echo ... ?>

Fragsworth
  • 28,413
  • 24
  • 76
  • 96
3

using short tags is generally frowned upon nowadays but it's still an option in the php.ini. It's fine, it's just poor coding style and has some repercussions if you use multiple dynamic languages.

Chuck Vose
  • 4,355
  • 21
  • 29
  • Not to mention short tags are deprecated as of php6 – Matt Dec 24 '09 at 18:15
  • Considering PHP 6 is far from finished (it's not even in alpha stage yet), things can change -- even if it probably will not for this specific point. – Pascal MARTIN Dec 24 '09 at 18:17
  • 1
    PHP6? who? what? where? when? – gahooa Dec 24 '09 at 18:46
  • 1
    I know this is an old answer but it may be useful to others if you updated it to reflect the current documentation. In particular, the fact that [since 5.4.0, `=` is enabled, even without short tags](http://www.php.net/manual/en/ini.core.php#ini.short-open-tag). – Tom Fenech Apr 20 '14 at 10:53