PHP else 语句无法正常工作.is_numeric和伊塞特


PHP else statement not working as it should.. is_numeric and isset

在下面的 If.. else 语句中,我试图首先查看是否有第 4 个面包屑(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)))) {
...

供将来参考,以及阅读本文的其他任何人。

这个问题在 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.中。问题解决了。它有效。