php 如果其他嵌入在 html 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4731810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:10:57  来源:igfitidea点击:

If else embedding inside html

phphtml

提问by user550265

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

在 html 中嵌入 if else 和 elseif 条件的正确方法是什么?

回答by coreyward

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 phpon the open tags does require that short_open_tagsis 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.

请注意,省略phpopen 标签确实需要short_open_tags在您的配置中启用,这是默认的. 相关的无花括号条件语法始终处于启用状态,并且无论此指令如何都可以使用。

回答by Tyler Treat

<?php if ($foo) { ?>
    <div class="mydiv">Condition is true</div>
<?php } else { ?>
    <div class="myotherdiv">Condition is false</div>
<?php } ?>

回答by Sparkles

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

在@Patrick McMahon 的回复中,此处的第二条评论($first_condition 为 false且 $second_condition 为 true)并不完全准确:

<?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; ?>

Elseiffires whether $first_conditionis true or false, as do additional elseifstatements, if there are multiple.

ELSEIF火灾是否$ first_condition是真的还是假的,因为这样做额外的ELSEIF语句,如果有多个。

I am no PHP expert, so I don't know whether that's the correctway to say IF this OR that ELSE thator 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.

我不是 PHP 专家,所以我不知道这是否是正确的说法,如果这个或那个,或者是否有另一种/更好的方式在 PHP 中编码,但这对那些正在寻找的人来说是一个重要的区别OR 条件与 ELSE 条件。

Source is w3schools.comand my own experience.

来源是w3schools.com和我自己的经验。

回答by Hitesh Sahu

 <?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 } ?>

回答by Patrick W. McMahon

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 141of the default php.ini file) you can replace your php open tag from <?phpto <?. This is not advisedas 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_tagis 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_tagturned off.

如果在您的 php.ini 属性中short_open_tag = true(通常可以在line 141默认的 php.ini 文件中找到),您可以将 php open 标记从 替换<?php<?不建议这样做,因为大多数实时服务器环境都关闭了此功能(包括许多 CMS,如 Drupal、WordPress 和 Joomla)。我已经在 Drupal 中测试了简短的开放标签,并确认它会破坏您的网站,所以坚持使用<?php. short_open_tag默认情况下并非在所有服务器配置中都处于启用状态,并且在针对未知服务器配置进行开发时不得假定为这样。许多托管公司已short_open_tag关闭。

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

short_open_tag在 stackExchange 中的快速搜索显示 830 个结果。https://stackoverflow.com/search?q=short_open_tag很多人对他们不应该玩的东西有问题。

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

对于某些服务器环境和应用程序,即使short_open_tag设置为true.

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

short_open_tag将在 PHP6 中删除,所以不要使用简写标签。

all future PHP versions will be dropping short_open_tag

所有未来的 PHP 版本都将下降 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

“多年来一直建议您不要使用短标签“捷径”,而是使用完整的标签组合。随着 XML 的广泛使用以及其他语言对这些标签的使用,服务器很容易变得混乱和最终会在错误的上下文中解析错误的代码。但由于此快捷方式已成为一项功能很长时间了,因此目前仍支持向后兼容,但我们建议您不要使用它们。” – Jelmer 2012 年 9 月 25 日 9:00 php:“short_open_tag = On”不起作用

and

Normally you write PHP like so: . However if allow_short_tagsdirective 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

通常你这样写 PHP: . 但是,如果 启用了allow_short_tags指令,您就可以使用: . 排序标签还提供了额外的语法:它等于 .

短标签可能看起来很酷,但事实并非如此。它们只会导致更多的问题。哦...和 ​​IIRC 他们将从 PHP6 中删除。Crozin 于 10 年 8 月 24 日 22:12 回答了 php short_open_tag 问题

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?

为了回答为什么部分,我引用 Zend PHP 5 认证指南:“短标签一度是 PHP 世界的标准;但是,它们确实有与 XML 标头冲突的主要缺点,因此,有点半途而废。” – Fluffy 2011 年 4 月 13 日 14:40 是否可以使用 PHP 短标签?

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.

这会起作用,但它被高度反对,因为它不被认为是清晰的,并且不是您使用这种格式的目的。如果您有一个 PHP 文件,其中有一段没有嵌入标签的 PHP 代码,那么您将使用括号格式。

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.

如果您是为自己编写此代码,则可以做您喜欢做的事情,但如果您与团队一起工作,建议在正确的情况下使用正确的格式。如果您在嵌入的 html/php 脚本中使用方括号,这是一种被解雇的好方法,因为没有人会想在您之后清理您的代码。IT 老板会关心代码的易读性,大学教授会对易读性进行评分。

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.

根据 duskwuff 的评论,尚不清楚是否不鼓励速记(根据 php 标准)。当我获得更多信息时,我会更新这个答案。但是基于网上找到的许多关于速记不利于可移植性的文档。我个人仍然不会使用它,因为它没有任何优势,并且您必须依赖于并非每个网络主机都启用的设置。