如何解决“可恢复的致命错误:类Closure的对象无法在……中转换为字符串”

当我执行此代码时出现此错误。我不知道该怎么办。请帮忙


<?php


$Q = strtoupper($_GET['q']);

$q = ucwords($_GET['q']); 

$result = mysqli_query($conn, "SELECT src FROM mytable WHERE '%$Q%' NOT LIKE 0 OR SRC LIKE '%$Q%'");


$total = mysqli_num_rows($result);

$numRows = function() 

{

    if($total <= 4){

        return 1;

    } else {

        return ($total / 4);

    }

};

if($row = mysqli_fetch_array($result)){ 

?>              

    <h2>Resultados para la búsqueda <?php echo "$q"?></h2>

    <h3>Número de resultados total: <?php echo "$total"?></h3>

<?php  

}


慕无忌1623718
浏览 118回答 2
2回答

泛舟湖上清波郎朗

根据错误,听起来您好像是在使用$numRows字符串,但这必须在代码中更远的地方,这是您尚未发布的内容。$numRows()我相信您可以调用函数,但是在内部$numRows,您可以使用$total。正如Zain Farooq所建议的那样,您也许可以,use ($total)但是最好$total将函数参数作为参数传递。例子:$total = mysqli_num_rows($result);$getNumRows = function($tot)&nbsp;{&nbsp; &nbsp; if($tot <= 4){&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return ($tot / 4);&nbsp; &nbsp; }};$result = $getNumRows($total);echo "I have {$result} rows.";我认为您正在执行的操作,在某处正在执行类似的操作echo $numRows,但是它必须是echo $numRows(),因此函数被调用了。

慕哥9229398

你的问题就在这里$numRows = (function() use ($total) {if($total <= 4){return 1;}else{&nbsp;return ($total / 4);}})();您必须在括号之间包装函数,如果要传递参数,则应使用 use()
打开App,查看更多内容
随时随地看视频慕课网APP