If else embedding inside html

前端 未结 5 1537
情话喂你
情话喂你 2020-12-12 15:35

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

5条回答
  •  春和景丽
    2020-12-12 16:11

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

    
      /*$first_condition is true*/
    
      /*$first_condition is false and $second_condition is true*/
    
      /*$first_condition and $second_condition are false*/
    
    

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

    • http://www.askapache.com/php/shorthand-short_open_tag.html
    • https://softwareengineering.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php

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

    
      /*$first_condition is true*/
    
      /*$first_condition is false and $second_condition is true*/
    
      /*$first_condition and $second_condition are false*/
    
    

    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

    
    

    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.

提交回复
热议问题