97

What is the correct way of embedding if else and elseif conditions inside html?

user550265
  • 3,171
  • 8
  • 44
  • 55

5 Answers5

196

I recommend the following syntax for readability.

<? if ($condition): ?>
  <p>Content</p>
<? elseif ($other_condition): ?>
  <p>Other Content</p>
<? else: ?>
  <p>Default Content</p>
<? endif; ?>

Note, omitting php on the open tags does require that short_open_tags is enabled in your configuration, which is the default. The relevant curly-brace-free conditional syntax is always enabled and can be used regardless of this directive.

coreyward
  • 77,547
  • 20
  • 137
  • 166
  • 6
    This example is missing the 'PHP' in the statements. – Adam Youngers Oct 31 '14 at 17:46
  • 3
    @AdamYoungers These are called short_open_tags, and while you might not be familiar with them, they're perfectly valid. – coreyward Oct 31 '14 at 19:06
  • 5
    I wasn't familiar with them. I tried using the above and it broke my page. A quick search and I see you have to enable this in your php.ini file... http://stackoverflow.com/questions/2185320/how-to-enable-php-short-tags – Adam Youngers Oct 31 '14 at 19:20
  • 2
    do not provide examples that assume special configurations. Assume default config. Please correct your answer to include the PHP in the tag. You can include how to setup this configuration and then show the difference but that is beyond the scope of the question. Other than that your answer is valid. – Patrick W. McMahon Feb 24 '15 at 19:44
  • 6
    I would highly suggest not using php short tags. It can add confusion, and as well cause random errors that can lead to developers pulling out their hair. – NateAGeek Dec 08 '15 at 17:16
  • 2
    @NateAGeek You are welcome to not use them, but opining on a 5 year old answer is unnecessary. – coreyward Dec 09 '15 at 15:34
52
<?php if ($foo) { ?>
    <div class="mydiv">Condition is true</div>
<?php } else { ?>
    <div class="myotherdiv">Condition is false</div>
<?php } ?>
Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
25

In @Patrick McMahon's response, the second comment here ( $first_condition is false and $second_condition is true ) is not entirely accurate:

<?php if($first_condition): ?>
  /*$first_condition is true*/
  <div class="first-condition-true">First Condition is true</div>
<?php elseif($second_condition): ?>
  /*$first_condition is false and $second_condition is true*/
  <div class="second-condition-true">Second Condition is true</div>
<?php else: ?>
  /*$first_condition and $second_condition are false*/
  <div class="first-and-second-condition-false">Conditions are false</div>
<?php endif; ?>

Elseif fires whether $first_condition is true or false, as do additional elseif statements, if there are multiple.

I am no PHP expert, so I don't know whether that's the correct way to say IF this OR that ELSE that or if there is another/better way to code it in PHP, but this would be an important distinction to those looking for OR conditions versus ELSE conditions.

Source is w3schools.com and my own experience.

pensebien
  • 506
  • 4
  • 16
Sparkles
  • 351
  • 3
  • 3
  • Are you sure? Just tested it with: `if(true){ echo 'here';} elseif (true){ echo 'second';} else { echo 'whaddup';}exit;` Output: "here" On PHP 5.5 – Philipp Jul 28 '16 at 16:18
5

You will find multiple different methods that people use and they each have there own place.

<?php if($first_condition): ?>
  /*$first_condition is true*/
<?php elseif ($second_condition): ?>
  /*$first_condition is false and $second_condition is true*/
<?php else: ?>
  /*$first_condition and $second_condition are false*/
<?php endif; ?>

If in your php.ini attribute short_open_tag = true (this is normally found on line 141 of the default php.ini file) you can replace your php open tag from <?php to <?. This is not advised as most live server environments have this turned off (including many CMS's like Drupal, WordPress and Joomla). I have already tested short hand open tags in Drupal and confirmed that it will break your site, so stick with <?php. short_open_tag is not on by default in all server configurations and must not be assumed as such when developing for unknown server configurations. Many hosting companies have short_open_tag turned off.

A quick search of short_open_tag in stackExchange shows 830 results. https://stackoverflow.com/search?q=short_open_tag That's a lot of people having problems with something they should just not play with.

with some server environments and applications, short hand php open tags will still crash your code even with short_open_tag set to true.

short_open_tag will be removed in PHP6 so don't use short hand tags.

all future PHP versions will be dropping short_open_tag

"It's been recommended for several years that you not use the short tag "short cut" and instead to use the full tag combination. With the wide spread use of XML and use of these tags by other languages, the server can become easily confused and end up parsing the wrong code in the wrong context. But because this short cut has been a feature for such a long time, it's currently still supported for backwards compatibility, but we recommend you don't use them." – Jelmer Sep 25 '12 at 9:00 php: "short_open_tag = On" not working

and

Normally you write PHP like so: . However if allow_short_tags directive is enabled you're able to use: . Also sort tags provides extra syntax: which is equal to .

Short tags might seem cool but they're not. They causes only more problems. Oh... and IIRC they'll be removed from PHP6. Crozin answered Aug 24 '10 at 22:12 php short_open_tag problem

and

To answer the why part, I'd quote Zend PHP 5 certification guide: "Short tags were, for a time, the standard in the PHP world; however, they do have the major drawback of conflicting with XML headers and, therefore, have somewhat fallen by the wayside." – Fluffy Apr 13 '11 at 14:40 Are PHP short tags acceptable to use?

You may also see people use the following example:

<?php if($first_condition){ ?>
  /*$first_condition is true*/
<?php }else if ($second_condition){ ?>
  /*$first_condition is false and $second_condition is true*/
<?php }else{ ?>
  /*$first_condition and $second_condition are false*/
<?php } ?>

This will work but it is highly frowned upon as it's not considered as legible and is not what you would use this format for. If you had a PHP file where you had a block of PHP code that didn't have embedded tags inside, then you would use the bracket format.

The following example shows when to use the bracket method

<?php
if($first_condition){
   /*$first_condition is true*/
}else if ($second_condition){
   /*$first_condition is false and $second_condition is true*/
}else{
   /*$first_condition and $second_condition are false*/
}
?>

If you're doing this code for yourself you can do what you like, but if your working with a team at a job it is advised to use the correct format for the correct circumstance. If you use brackets in embedded html/php scripts that is a good way to get fired, as no one will want to clean up your code after you. IT bosses will care about code legibility and college professors grade on legibility.

UPDATE

based on comments from duskwuff its still unclear if shorthand is discouraged (by the php standards) or not. I'll update this answer as I get more information. But based on many documents found on the web about shorthand being bad for portability. I would still personally not use it as it gives no advantage and you must rely on a setting being on that is not on for every web host.

Community
  • 1
  • 1
Patrick W. McMahon
  • 3,488
  • 1
  • 20
  • 31
  • 1
    "in PHP6"? PHP 6 doesn't exist, and never will. The next major version is PHP 7, and is not expected to change the status of short_open_tags. –  Feb 26 '15 at 20:20
  • @duskwuff It's possible you may be correct about them jumping to PHP7 and skipping PHP6. but they are considering short_open_tags as discouraged in PHP5 and will be deprecated. – Patrick W. McMahon Feb 26 '15 at 20:48
  • 2
    Where are you getting the idea that they are "discouraged", or "will be deprecated"? The documentation says absolutely nothing to suggest this. http://php.net/manual/en/ini.core.php#ini.short-open-tag –  Feb 26 '15 at 20:57
  • @duskwuff I'm merely referencing documents others have stated. see for example http://stackoverflow.com/questions/22554753/php-5-5-short-open-tag-on-security-hole and for what i have said about portability and how you can't assume every server has it turned on, I'm not incorrect and how everyone on the web is saying not to use short hand I have to side with the numbers provided. There is simply to many people saying not to use them rather then using them. along with no advantage for using them. – Patrick W. McMahon Feb 26 '15 at 21:03
  • 1
    @PatrickW.McMahon You haven't provided any numbers. You're linking to a single StackOverflow answer that's hardly an authority, and expanding it to “everyone on the web”. Get a grip. – coreyward Feb 26 '15 at 21:06
  • @coreyward given your exceptional persuasion skills. I'm having doubt about my original stance on the php standards being discouraged. looking back on my references it looks more like people are discouraging it not the php folks. I have updated my answer. – Patrick W. McMahon Feb 26 '15 at 21:14
2
 <?php if (date("H") < "12" && date("H")>"6") { ?>
   src="<?php bloginfo('template_url'); ?>/images/img/morning.gif" 
 <?php } elseif (date("H") > "12" && date("H")<"17") { ?>
 src="<?php bloginfo('template_url'); ?>/images/img/noon.gif" 
  <?php } elseif (date("H") > "17" && date("H")<"21") { ?>
   src="<?php bloginfo('template_url'); ?>/images/img/evening.gif" 
  <?php } elseif (date("H") > "21" && date("H")<"24") { ?>
  src="<?php bloginfo('template_url'); ?>/images/img/night.gif" 
  <?php }else { ?>
  src="<?php bloginfo('template_url'); ?>/images/img/mid_night.gif" 
  <?php } ?>
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154