3

In .net we've got the following to write a variable (well, it's ToString() method) in an asp.net page:

<%= myString %>

Is there anything like this in php? (I'm tired of typing "ehco" instead of "echo");

David Murdoch
  • 87,823
  • 39
  • 148
  • 191
  • It's an extremely bad practice to do such thing. It's not valid on every environment, you'll find yourself having more problems than answers. Type the whole echo, it takes 1 second and may save you many hours of debugging. – Vincent Savard Nov 19 '10 at 21:51
  • 1
    @Vincent but have you ever seen a server where they've been disabled? I mean, in the real world? I avoid short open tags for the same reason as you, but I find myself asking whether it's really a problem to use them. – Pekka Nov 19 '10 at 21:54
  • most IDEs allow you to make macros of some sort: make a macro that echos 'echo'. – zzzzBov Nov 19 '10 at 21:55
  • @Pekka: Yes, way too often. I post on another forum (mostly for beginners), and we have to answer this question a few times every week because they don't understand why it doesn't work. As zzzzBov suggested, I'd definitely use a macro that converts = to – Vincent Savard Nov 19 '10 at 21:55
  • @Pekka I've never used a server where short-tags were enabled by default. I could always change that, but why would I? I'm not *that* lazy. – zzzzBov Nov 19 '10 at 21:56
  • 2
    @Vincent okay; I for one have never seen a server where short open tags were disabled. I'm seriously thinking about returning to them - the macro idea is certainly the best way to go about it though – Pekka Nov 19 '10 at 21:56
  • extremely is pretty extreme, man. The "not being valid on every environment" argument is kinda a lame, IMO. If the shorthand isn't going to work in my environment then clearly I'm not in control of that environment...so I don't really care (in this instance). – David Murdoch Nov 19 '10 at 21:57
  • 1
    @Vincent It is arguably *mildly* bad practice *at worst*. "Extremely bad practice" is your own spin. Many people continue to use them, and there is *absolutely* no reason to avoid using them in your output-oriented templates. "Some servers might not have them" is a completely bogus reason. Some servers might not have GD, or cURL, or any one of countless optional PHP features. You don't avoid those features, you use them and then choose a host which provides them. – user229044 Nov 19 '10 at 21:59
  • @David and meagar : I'm not gonna argue forever, this is based on my own experience and I'm not going to haunt you if you use short tags. With today's IDEs, you can easily make a macro that replaces = to – Vincent Savard Nov 19 '10 at 22:05
  • @Vincent Readable code is *far* more important than saving keystrokes. I find `= $name ?>` far more readable than ``, especially when mixed with HTML. Using a macro misses the point entirely. – user229044 Nov 19 '10 at 22:10
  • @meagar : I don't. This is subjective, I'm talking about real impact short tags have on code (i.e. may not always work). [troll]If we go about more readable code, might as well drop PHP for Python![/troll] – Vincent Savard Nov 19 '10 at 22:12
  • @Vincent As I said above, "may not always work" is a bogus argument. `mod_rewrite` doesn't work everywhere, neither does MySQL. Are you recommending that people avoid those? If you only use features that are universally available, you'll be writing in PHP3 forever. – user229044 Nov 19 '10 at 22:17
  • @meagar : I really don't understand what this have to do with anything, therefore I'll stop answering to you. I really stated my points, so did you, this is going nowhere. – Vincent Savard Nov 19 '10 at 22:25

3 Answers3

6

Provided you have short_open_tags enabled in php.ini, you can use:

<?= $myString ?>

Debate rages about whether this is a good idea.

It is also possible to use ASP-style tags by enabling asp_tags in php.ini, but they are not recommended (read, deprecated and I believe being removed in PHP6):

<%= $myString %>
Community
  • 1
  • 1
user229044
  • 232,980
  • 40
  • 330
  • 338
  • It's not deprecated and will [not be removed in PHP 6](http://stackoverflow.com/questions/3533194/php-short-tags-really-being-deprecated-in-php6-closed). But it's correct it's not recommended – Pekka Nov 19 '10 at 21:53
  • @Pekka I was talking specifically about ASP-style tags, not short_open_tag – user229044 Nov 19 '10 at 21:53
  • 1
    I will add as a comment (to leave my answer slightly more neutral) that I personal believe `short_open_tags` are amazingly useful, and the only reason I'd consider PHP as a template langauge. `` is hideous, and I will *never* use it in my templates. – user229044 Nov 19 '10 at 21:55
  • You just saved me from having to type out 22 ` – David Murdoch Nov 19 '10 at 22:04
3

<?= $var ?> will do it.

eaj
  • 2,576
  • 1
  • 20
  • 42
  • so, I would mark this as accepted because you answered 9 seconds faster...but meagar told me how to turn it on...which i needed. Thanks though. – David Murdoch Nov 19 '10 at 22:00
1

<?= $myString ?> (assuming PHP's short tags are enabled)

ceejayoz
  • 176,543
  • 40
  • 303
  • 368