Php shorthand if,else help

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
      .......
      .......
      .......

yes

I don’t see much difference though. Just one single character per statement
<?php }else{ ?>
vs.
<?php else: ?>

http://www.php.net/manual/en/control-structures.alternative-syntax.php

ahhh just what I was looking for.

When reading a bunch of HTML this

<? endif ?>

is easier to spot that this

<? } ?>

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.

depending on the situation it may make sense to call a function depending on the condition that returns the markup.

e.g


if($animal == 'elephant') {
   echo elephant_info();
}
else {
   echo other_info();
}

Then your presentation logic is moved into its own space.

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 :stuck_out_tongue:

No problem with
<?php if(): ?> vs <?php if(){ ?>

But care should be taken with <? if() ?> short tags which may mess with xml code(discouraged to use so).

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 “<?=”

It hasn’t been mentioned, but you need to end an if block with the endif keyword when using the alternate syntax:


<?php if (...): ?>
<lots />
<of />
<html />
<?php else: ?>
<lots />
<of />
<html />
<?php endif; ?>

I personally do it this way, but don’t tell anyone here about it if you don’t want to start yet another protracted argument over short tags:


<?if (...) : ?>
<lots />
<of />
<html />
<?else: ?>
<lots />
<of />
<html />
<?endif; ?>

<?php }else{ ?>
vs.
<?php else: ?>

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.

An XML document may contain one or more processing instructions that start with <?nameOfLanguage.

but is about to be deprecated for PHP 6.

prooflink please.

There has been some discussion about it, but no official decision made to deprecate short tags.

http://news.php.net/php.internals/43695

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?

I totally agree on both points.

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.