如果否则嵌入在html中

use*_*265 64 html php

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

cor*_*ard 146

为了便于阅读,我建议使用以下语法.

<? if ($condition): ?>
  <p>Content</p>
<? elseif ($other_condition): ?>
  <p>Other Content</p>
<? else: ?>
  <p>Default Content</p>
<? endif; ?>
Run Code Online (Sandbox Code Playgroud)

注意,省略phpopen标记确实需要short_open_tags在配置中启用,这是默认配置.始终启用相关的无大括号的条件语法,无论此指令如何,都可以使用它.

  • 这个例子缺少语句中的'PHP'. (4认同)
  • 我不熟悉他们.我尝试使用上面的内容,它打破了我的页面.快速搜索,我发现你必须在你的php.ini文件中启用它... http://stackoverflow.com/questions/2185320/how-to-enable-php-short-tags (4认同)
  • 我强烈建议不要使用php短标签.它可能会增加混乱,并导致随机错误,导致开发人员拔掉他们的头发. (4认同)
  • @AdamYoungers这些被称为short_open_tags,虽然您可能不熟悉它们,但它们完全有效. (3认同)
  • 请勿提供采用特殊配置的示例。假设默认配置。请更正您的答案以将 PHP 包含在标记中。您可以包括如何设置此配置,然后显示差异,但这超出了问题的范围。除此之外,你的答案是有效的。 (2认同)
  • @NateAGeek欢迎您不要使用它们,但是不需要使用5岁的答案. (2认同)

Tyl*_*eat 41

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


小智 13

编辑:我希望记下@ Patrick的回答,但显然StackOverflow认为我的声誉不够好.我觉得这是一个重要的注意事项,因为登陆这里的人正在这样学习.如果因为它没有回答问题而被删除,那就这样吧.如果帕特里克可以纠正他的答案,那么删除它不会有害.

我看到这是一个古老的问题/答案,但它在Bing搜索中排名很高,所以我不得不为那些登陆这里的PHP迷惑的灵魂添加这个评论.

在@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; ?>
Run Code Online (Sandbox Code Playgroud)

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

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

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


Pat*_*hon 6

你会发现人们使用多种不同的方法,并且每种方法都有自己的用处。

\n\n
<?php if($first_condition): ?>\n  /*$first_condition is true*/\n<?php elseif ($second_condition): ?>\n  /*$first_condition is false and $second_condition is true*/\n<?php else: ?>\n  /*$first_condition and $second_condition are false*/\n<?php endif; ?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

在 stackExchange 中快速搜索short_open_tag显示 830 个结果。https://stackoverflow.com/search?q=short_open_tag \n很多人都遇到了一些他们不应该玩的东西的问题。

\n\n

short_open_tag对于某些服务器环境和应用程序,即使设置为.php,简写 php 打开标记仍然会使您的代码崩溃true

\n\n

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

\n\n

所有未来的 PHP 版本都将被删除short_open_tag

\n\n\n\n
\n

“多年来,我们一直建议您不要使用短\n 标签“快捷方式”,而应使用完整的\n 标签组合。随着 XML 的广泛使用以及其他\n 工具对这些标签的使用,\n语言,服务器可能会很容易混淆并最终在错误的上下文中解析错误的代码。但是由于这种快捷方式已经存在很长时间了,因此目前仍然支持向后\n兼容性,但我们建议您不要使用它们。” \xe2\x80\x93 Jelmer 2012 年 9 月 25 日 9:00 \n php:“short_open_tag = On”不工作

\n
\n\n

\n\n
\n

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

\n\n

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

\n
\n\n

\n\n
\n

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

\n
\n\n

You may also see people use the following example:

\n\n
<?php if($first_condition){ ?>\n  /*$first_condition is true*/\n<?php }else if ($second_condition){ ?>\n  /*$first_condition is false and $second_condition is true*/\n<?php }else{ ?>\n  /*$first_condition and $second_condition are false*/\n<?php } ?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

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.

\n\n

The following example shows when to use the bracket method

\n\n
<?php\nif($first_condition){\n   /*$first_condition is true*/\n}else if ($second_condition){\n   /*$first_condition is false and $second_condition is true*/\n}else{\n   /*$first_condition and $second_condition are false*/\n}\n?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

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.

\n\n

UPDATE

\n\n

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.

\n

  • 您从哪里得到“不鼓励”或“将被弃用”的想法?文档中完全没有提到这一点。http://php.net/manual/en/ini.core.php#ini.short-open-tag (2认同)