2

Man, I could swear I saw it somewhere, the useful syntax for shortening the <?php echo $var ?> construct, and I think it was <?=$var?> but it doesn't work. I've searched about half an hour now but can't find the website again that told me about it, and I can't figure out the correct syntax.

Am I hallucinating or is there really a syntax like this?

<div style="background-color:<?=$backgroundColorFromPhp?>">
    Hello <?=$name?>!
</div>
7
  • 7
    php.net/manual/en/ini.core.php#ini.short-open-tag and php.net/manual/en/ini.core.php#ini.asp-tags Commented Mar 5, 2012 at 23:04
  • Tomalak's comment is the right answer! Commented Mar 5, 2012 at 23:08
  • @Tomalak I'm worrying about my googling-skills .. :/ Would you mind making an Answer? Thx Commented Mar 5, 2012 at 23:09
  • 2
    @scibuff - reason for not using the short open tag is that it may not be enabled on every server... if you can guarantee control of your own servers/PHP ini file, and can ensure that it's always enabled, then there's no reason not to use it Commented Mar 5, 2012 at 23:22
  • 1
    The only problem with short tags is they can conflict with xml syntax but I would tend to avoid xml rather than shorttags Commented Mar 5, 2012 at 23:27

5 Answers 5

6

PHP has the short_open_tag directive in php.ini which enables this syntax:

<? PHP code here; ?>

as opposed to <?php PHP code here; ?>.

It also enables

<?= variable ?>

as a shorthand for <? echo variable; ?>. The docs note that as of PHP 5.4.0 <?= will be enabled regardless of the short_open_tag setting.

Very similar is asp_tags. It enables the classic ASP-style syntax:

<% PHP code here; %>
<%= variable %>

which is where the echo-shorthand syntax originally came from. Side note: In classic ASP <%= ... would replace the rather verbose form:

<% Response.Write variable %>
Sign up to request clarification or add additional context in comments.

2 Comments

has PHP copied ASP echo-shorthand syntax?
@yes123 I've always believed it went that way. I admit I'm not sure.
2

When short_open_tag is enabled,

<?= $backgroundColorFromPhp ?>

is a shortcut for

<?php echo $backgroundColorFromPhp; ?> 

however, if short_open_tag is disabled,

<?= $backgroundColorFromPhp ?>

will also be disabled (prior to PHP 5.4).

Comments

1

Yes, that's correct, given some caveats.

First, you have to have short tags on. In my experience, this is the default configuration, as noted by the documentation above;

Second, you need the file to be parsed by PHP. If the file is a .html file, it won't be parsed automatically by the php engine. You'll need to run it through somehow, or tell php to read .html files by default.

Finally, I like to throw that end statement semicolon in there (making it <?=$name;?>), but that's a point of preference, I believe.

2 Comments

Most explanative answer, thank you. :) Don't get why it wasn't enabled, modifing the php.ini didn't work, but setting the value from WAMP did. Edit: Tomalek was first with his comment, sorry. ;)
@NiklasR Perfectly fair, glad to be of assistance!
1

there is really a syntax like that on php and it would only work when you set your server to make it work it's called shorthand but this technique is strongly discouraged because there are some servers on which this is disabled and may provide some unidentified errors when used mistakenly. I think I first saw this being used on wordpress. ;)

1 Comment

thanks for info @Gordon didn't thought that they have included it on the latest release, eventually I really think most people should really update their servers to this new version. ;)
0

What you are trying to do should be done as follows

     <div style="background-color:<?=$backgroundColorFromPHP ?>">
          Hello <?=$name ?>!
    </div>

In short, leave a space after the before the last ? mark

And please strictly avoid echoing your html contents. Thats simply a bad style. Always keep dynamics of your php code separated from the html elements.

2 Comments

This isn't necessary in any implementation I've encountered.
The only differnce here is that the closing tags ?> are seperated with a whitespace from the variable-name? That does not solve it, short_open_tags is the solution. :) And that extra whitespace doesnt make any differnce actually..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.