PHP else语句没有显示

时间:2018-04-20 17:51:29

标签: php if-statement echo

我在WP帖子中有一个名为" professional的自定义字段。"如果这个职业是RN,那么我想要显示一段文字。如果专业不是RN,那么我想要显示另一段文字。

我添加了以下代码来执行此功能:

        <?php if ( $profession = 'RN' ) {
        echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
    } else {
        echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
    } ?>

问题是,即使帖子在专业领域没有RN,第一个echo语句也会显示。如果我故意破坏if变量,那么它默认为全面的第二个echo语句。我无法让帖子动态响应一个与另一个。

我是否遗漏了此代码中导致一个或另一个显示所有内容而不是基于我尝试设置的参数的内容?

3 个答案:

答案 0 :(得分:0)

你需要在你的比较中进行比较。

=  is used to assign values
== is used for comparison, checks if value is equal
=== is used for strict comparison, checks if value and type are equal.

所以你的php if语句看起来应该是这样的

if ( $profession == 'RN' ){
    code
}else{
    code
}    

答案 1 :(得分:0)

请在写作前阅读如何使用条件。

http://php.net/manual/en/control-structures.if.php

 <?php if ( $profession == 'RN' ) {
        echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
    } else {
        echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
    } ?>

答案 2 :(得分:-1)

<?php if ( $profession == 'RN' ) {
    echo '<li>Minimum 2 years experience</li><li>Current license in this      state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li>   <li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
    echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>