tags:

views:

82

answers:

3

So I have the following line...

<?=$variable?>

Is there a way to change it to long tags without typing a full echo statement?

<?php=$variable?> doesn't work

I know that <?php echo $variable ?> will work, but is there a shorter way to do it?

+1  A: 

What?

The short way to do it is <?= ... ?>. Which is equivalent to <?php echo ... ?>.

That's all there is to it.

jtbandes
Wait, so do PHP short tags need to be turned on for <?= ... ?> ? I'm trying to avoid short tags is all.
Chris
Yes, short tags includes all variants of `<?` without `php`
Mike B
@Mike B, `asp_tags` allows for `<%` and `<%=`, which mimic `<?` and `<?=`, respectively.
strager
@strager that's entirely irrelevant, nobody's even asking about asp style tags.
@strager asp style tags != short tags. The asker said he was trying to avoid short tags. I assume he would also want to avoid the very rare asp tags as well.
Mike B
@Mike B, The statement "short tags includes all variants of `<?` without `php`" is incorrect. Sorry I wasn't being clear in stating that.
strager
@strager Can you expand on that? What short tag consists of `<?` and `php`?
Mike B
@Mike B, A 'variant of `<?` without `php`' is `<%`. `<%` is not included in `short_tags`; it is included in `asp_tags`.
strager
@strager But I said said variants of short tags (which is really `<? ?>` and `<?= ?>`), not all possible php tags. ASP tags are not inclusive in short tags AFAIK.
Mike B
@Mike B, You said "variants of `<?`", not "variants of short tags". Also, there are either no "variants of short tags", or there are, and the only variants in PHP I see are `<%` and `<%=`.
strager
@strager I think we're talking about two different things :p. My lack of decent grammar is probably my flaw. I'll just leave it at that since the significance of this discussion is rather unimportant. See you around SO!
Mike B
+8  A: 

Is there a way to change it to long tags without typing a full echo statement?

No

Edit: For Strager http://bugs.php.net/bug.php?id=27448&amp;edit=1

Mike B
+1. Simple way to answer :P
Jimmie Lin
References, please?
strager
+2  A: 

The shorter way is to use tokenized replacements (via a templating system, like Smarty) instead of interpolating between php and html syntax. Tokens are cleaner in general, and they don't have to be complicated.

Your HTML would look like:

<html>
<head>
    <title>%title_token%</title>
</head>
<body>
    <div id="content">%content_token%</div>
    <div id="footer">%footer_token%</div>
</body>
</html>

etc. Different templating systems use different tokens (Smarty uses a several-step define, parse, compile), but all you really need is str_replace:

str_replace($template, '%title_token%', 'Super Awesome Webpage');
banzaimonkey
PHP is faster. Always. And PHP doesn't need much learning, because you already know it. (I can't say much for designers, however.)
strager
+1 for Smarty. Some people love it, others seem to avoid at all costs. To each his own :)
Mike B
@strager By "faster" I meant "takes less time to write / read", but changed to avoid confusion.
banzaimonkey