检查函数返回值而不再执行它

时间:2013-06-20 08:08:34

标签: php function return

如何获取函数返回值而不再执行它?

我使用了这个但是再次执行这个函数

我的职能:

function article()
{
    if($_GET['action'] == "article" && !empty($_GET['id']))
    {
        $id = intval($_GET['id']);
        $article = array();
        $selectArticle = mysql_query("SELECT * FROM articles WHERE id='$id'");
        $rowArticle = mysql_fetch_array($selectArticle);

        $id = $rowArticle['id'];
        $title = stripcslashes($rowArticle['title']);
        $category = stripcslashes($rowArticle['category']);
        $image = stripcslashes($rowArticle['image']);
        $description = stripcslashes($rowArticle['description']);
        $full_description = stripcslashes($rowArticle['full_description']);
        $keywords = stripcslashes($rowArticle['keywords']);
        $url = "/article/" . $rowArticle['id'] . "/" . str_replace(" ","-",stripcslashes($rowArticle['title']));
        $article = array('id' => $id, 'title' => $title, 'category' => $category, 'image' => $image, 'description' => $description, 'full_description' => $full_description, 'keywords' => $keywords, 'url' => $url);
        mysql_query("UPDATE articles SET visits=visits+1 WHERE id='$id'");
    }
    return $article;
}

如何检查

if (article() != null)
{
    $article = article();
    return $article['title'];
}

3 个答案:

答案 0 :(得分:2)

这应该做你想要的事情

if (null !== ($article = article())) {
    return $article['title'];
}

这是“同时”分配和比较。 首先,评估此部分:($article = article())。它会生成null值或array,该值存储在$article中 然后,结果(nullarray)由if结构评估:if (null !== $article)并恢复正常流程。

答案 1 :(得分:1)

喜欢这样

$article_var = article();

if ($article_var!=null)
{
      //do stuff
      //return $article['title'] // etc
}

答案 2 :(得分:0)

稍微修改一下。

$article = article();
return $article != null ? $article['title'] : null;