PHP else语句不能正常工作.. is_numeric和isset

时间:2013-01-23 04:10:16

标签: php if-statement

在下面的If .. else语句中,我试图看看是否有第四个痕迹(URI数组4),然后查看它是否是一个数字..

但两者总是返回true,数字,字母或空..

谁能告诉我为什么?

// checks the third breadcrumb contained within the breadcrumbs class to see
// if it is an article. Then uses a recursive function to search the first
// and second breadcrumbs to see if they exist within their respective menu
// arrays. finally, if that validates, then the fourth breadcrumb is checked
// to see if it exists (not empty) AND if it is a number.
else 
if (($this->breadcrumbs->getCrumb(3)=='article' && 
     $array_helper->recValueReturn($this->breadcrumbs->getCrumb(2),
     $this->getNavTwo()) && 
     $array_helper->recValueReturn($this->breadcrumbs->getCrumb(1),
     $this->getNavOne())) || ($this->breadcrumbs->getCrumb(4)) && 
     is_numeric($this->breadcrumbs->getCrumb(4))) {
...

以下总是验证为False:

else
if (($this->breadcrumbs->getCrumb(3)=='article'&&
     $array_helper->recValueReturn($this->breadcrumbs->getCrumb(2),
     $this->getNavTwo()) &&
     $array_helper->recValueReturn($this->breadcrumbs->getCrumb(1),
     $this->getNavOne())) &&
     (array_key_exists($this->breadcrumbs->getCrumb(4),
      $this->breadcrumbs->getCrumbs())&&
      is_numeric($this->breadcrumbs->getCrumb(4)))) {
...

1 个答案:

答案 0 :(得分:1)

供将来参考,以及其他阅读此内容的人。

问题在BreadCrumbs类中解决了。

上一页,breadcrumbs有一个包含爆炸URI值的私有数组。我使用以下方法检索了一个url索引:

public function getCrumb($x) { return $this->breadcrumbs[$x] }

但是,如果我直接修改此getter以包含isset:

public function getCrumb($x) {
    if (isset($this->breadcrumbs[$x])) {
        return $this->breadcrumbs[$x];
    } else {
            return null;
    }
}

然后在OP中使用第一个 else..if ..问题解决了。它有效。