is there a php shorthand that makes it easy to display html code? The way I’m doing it makes me use a lot of opening and closing ‘{’, ‘}’, and ‘<?’ tags. like so…
<?php
if($var > 0){
?>
LOTS OF HTML HERE
.......
.......
.......
<?php
}
else{
?>
LOTS OF HTML HERE
.......
.......
.......
<?php
}
?>
is there a way to just say
<?php if($var > 0): ?>
LOTS OF HTML HERE
.......
.......
.......
<?php else: ?>
LOTS OF HTML HERE
.......
.......
.......
The difference really doesn’t come into its own until you’re working with large files. In the end though it is a convenience issue.
I use braceless syntax with short tags in my view files primarily because I find it easier to read. And I’m quite peeved that short tags are on the deprecation list.
Oh no. Bunch of functions isn’t good for template.
I prefer to have all markup in one place. Along with appropriate logic.
And I am using braceless syntax with short tags myself
I hear this parroted out a lot but the reality is this - <? is used once only in xml documents: at the top for the xml doctype. In that event echo it
<?= ‘<?’ ?>
Dealing with that one weird case on rare occasions is no excuse for trying to argue that we should type out “<?php echo” over and over making templates very hard to read compared to “<?=”
To me the first option looks very ugly. Alternative syntax is the way to go. This is even better:
<? else: ?>
but is about to be deprecated for PHP 6. Most often I use Smarty so I’m not affected by short/long php tags and I like the simple syntax:
{if …}
some html…
{else}
some html B…
{/if}
Smarty 3 is in active development (beta 5 currently) and offers many improvements over the obsolete version 2. Those not hating this template engine will find it very satisfying.
Well if they’re on the bubble I vote not to do it. I use them extensively. My framework has to have .htaccess mod_rewrite set, and php_short_tags can be enabled from there so portability isn’t a concerned (even if turned off in php.ini the .htaccess flag can turn them on).
Didn’t know about the <?nameoflanguage business in XML, but honestly that can still be handled with <?= ‘<?nameoflanguage’ so why inconvenience the rest of the functionality of the system over a rarely encountered corner case?
The portability argument against short tags doesn’t wash with me because it’s so easy to enable them. I have never encountered a hosting company that refused to enable them.
Echoing the XML declaration as a string isn’t so awkward that it warrants eliminating a very useful programming shortcut. Just copy and paste that one line of code if you don’t want to type it, for goodness sakes! Besides, PHP already supports ASP-style tags for those who prefer them.
The processing instructions I mentioned are like regular PHP tags except they can contain code in any language, not just PHP. It works about the same as using the <?php tag to demark PHP embedded in HTML.