为什么这总是评估为假?

时间:2015-08-27 19:02:02

标签: php wordpress

我有一段代码我做了

abstract class WorkTypes
{
    const Portfolio = 0;
    const Study = 1;
}

function get_our_work ( $atts )
{

   // see https://codex.wordpress.org/Shortcode_API

    $work_type = (strcmp($atts['workType'],'ports') === 0) ?  WorkTypes::Portfolio : WorkTypes::Study;

    include "page-content/our_work.php";
}

并且理由$work_type始终评估为WorkTypes::Study,即使我100%确定$atts['workType']等于'ports'。所以我知道(strcmp($atts['workType'],'ports') === 0)始终在评估false。为什么呢?

1 个答案:

答案 0 :(得分:0)

你正在对一个数字进行布尔评估吗?我有很长一段时间没有做过PHP,但我很确定你必须要有类似的东西:

$ work_type =(strcmp($ atts ['workType'],'ports')<> 0)? WorkTypes :: Study:WorkTypes :: Portfolio;

如果你的$ atts ['workType']与'ports'不匹配,那么读取,那么WorkTypes :: Study它确实匹配所以使用WorkTypes :: Portfolio,无论哪个比较字符串不同。