-1

如何在不再次执行的情况下获取函数返回值?

我用过这个,但这个执行功能再次

我的功能:

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'];
}
4

3 回答 3

2

这应该做你想做的

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

这会进行“同时”分配和比较。首先,对这部分进行评估:($article = article()). 它产生一个null值或array存储在$article. 然后由 if 结构评估
其结果(null或):并恢复正常流程。arrayif (null !== $article)

于 2013-06-20T08:10:28.750 回答
1

像这样

$article_var = article();

if ($article_var!=null)
{
      //do stuff
      //return $article['title'] // etc
}
于 2013-06-20T08:11:06.397 回答
0

稍微修改一下检查。

$article = article();
return $article != null ? $article['title'] : null;
于 2013-06-20T08:10:34.723 回答