Unless otherwise embedding inside html

What is the correct way to nest if else and elseif conditions inside html?

+63
html php
Jan 19 2018-11-11T00:
source share
6 answers

See this and its alternate , which tends to be more readable if embedded inside html blocks.

+13
Jan 19 2018-11-11T00:
source share

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. Disabling php in open tags requires short_open_tags , which is the default , to be enabled in your configuration. The corresponding conditional syntax, depending on the curly brace, is always included and can be used independently of this directive.

+145
Jan 19 '11 at 4:45
source share
 <?php if ($foo) { ?> <div class="mydiv">Condition is true</div> <?php } else { ?> <div class="myotherdiv">Condition is false</div> <?php } ?> 
+40
Jan 19 2018-11-11T00:
source share

In @Patrick McMahon's answer, 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*/ <?php elseif ($second_condition): ?> /*$first_condition is false and $second_condition is true*/ <?php else: ?> /*$first_condition and $second_condition are false*/ <?php endif; ?> 

Elseif launches whether $ first_condition is true or false, like the optional elseif statements, if there are several.

I'm not a PHP expert, so I don’t know if it’s right to say IF it is OR, FURTHER, or there is another / better way to code it in PHP, but that would be an important difference for those who are looking for OR conditions compared to others conditions.

Source - w3schools.com and my own experience.

+11
Jul 28 '16 at 16:04
source share
  <?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 } ?> 
+2
Apr 23 '17 at 16:41
source share

You will find several different methods that people use, and each of them has its 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 your php.ini attribute has short_open_tag = true (usually it is on line 141 the php.ini file by default), you can replace your open php tag with <?php with <? . This is not recommended , as most real-time slices are disabled (including many CMS such as Drupal, WordPress, and Joomla). I already tested short open tags in Drupal and confirmed that it will break your site, so stick with <?php . short_open_tag is not enabled by default in all server configurations and should not be accepted as such when developing for unknown server configurations. Many hosting companies have disabled short_open_tag .

A quick search for short_open_tag on stackExchange shows 830 results. https://stackoverflow.com/search?q=short_open_tag Many people have problems with something that they simply shouldn't play with.

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

short_open_tag will be removed in PHP6, so do not use short tags.

all future PHP versions will be discarded short_open_tag

“For several years, it was recommended that you do not use the short tag“ short cut ”and instead use the full tag combination. With the widespread use of XML and the use of these tags in other languages, the server can easily get confused and ultimately parse the wrong code in the wrong context. But since this short slice has been a feature of such a long time, it is still still supported for backward compatibility, but we recommend that you not use them. " - Jelmer Sep 25 '12 at 9:00 php: "short_open_tag = On" does not work

and

Usually you write PHP as follows :. However, if enable_short_tags, you can use :. Sort tags also provide additional syntax: which is equal.

Short tags may seem cool, but they aren’t. They cause only more problems. Oh ... and IIRC they will be removed from PHP6. Krozin answered on August 24 10 at 22:12 Problem with php short_open_tag

and

To answer the question why I am quoting Zend PHP 5: "Short tags were for some time the standard in the PHP world, however, they have the main drawback of the inconsistency with the XML headers and, therefore, are somewhat marginalized." - Fluffy Apr 13 '11 at 14:40 Are short tags suitable for PHP?

You can also see how 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 deprecated because it is not considered legible and is not what you would use this format for. If you had a PHP file in which you had a block of PHP code in which there were no inline tags inside, you should 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 do this code for yourself, you can do what you like, but if you work with a team at work, it is recommended that you use the correct format for the right circumstance. If you use brackets in the built-in html / php scripts, this is a good way to fire, since no one will want to clear your code after you. IT executives will take care of the legibility of the code and the degree of college professor in readability.

UPDATE

based on duskwuff's comments, it's still unclear if shorthand is not recommended (by php standards) or not. I will update this answer when I receive more information. But based on many documents found on the Internet about reducing the harm to portability. Anyway, I personally will not use it, since it does not give advantages, and you should rely on a setting that is not enabled for each web host.

0
Feb 26 '15 at 18:46
source share



All Articles