在调用DemoSlideListing($apiobj,'tech',12,2)
之后它会打印函数的echo部分,但是我想在这个函数之外使用变量$TotalResults
,我想在页面的任何地方。我怎样才能做到这一点。提前致谢。请帮忙
function DemoSlideListing($apiobj, $query, $per_page, $page) {
$data = $apiobj->search_slides($query, $per_page, $page);
foreach($data as $key) {
$title = $key['TITLE'];
$title2 = substr($title, 0, 35);
$TotalResults = $key['TOTALRESULTS'];
echo '<td valign="top"><div id="slide_thumb">
</div>
<div id="slide_thum_des"><strong>Views :</strong> '.$info['VIEWS'].'<br />
<a href="'.$key['DOWNLOADURL'].'">'.$title2.'....</a></div>
</td>';
}
}
答案 0 :(得分:2)
从函数中返回它。请查看PHP documentation on function return values.
function DemoSlideListing($apiobj,$query,$per_page,$page){
$data = $apiobj->search_slides($query,$per_page,$page);
// Declare $TotalResults as an array
$TotalResults = array();
foreach ($data as $key){
$title = $key['TITLE'];
$title2 = substr($title, 0, 35);
// Append current value to TotalResutls
$TotalResults[] = $key['TOTALRESULTS'];
echo '<td valign="top"><div id="slide_thumb">
</div>
<div id="slide_thum_des"><strong>Views :</strong> '.$info['VIEWS'].'<br />
<a href="'.$key['DOWNLOADURL'].'">'.$title2.'....</a></div>
</td>';
}
// Return the value
return $TotalResults;
}
// Call as:
$totalresults = DemoSlideListing($apiobj,$query,$per_page,$page);
// $totalresults holds the array that $TotalResults held at the time the function execution completed.
print_r($totalresults);