请问一下为什么我print_r 输出的值是对的,然后想马值 $location return就是return不出来.谢谢,望高手赐教!!! 急

suchengfei 2007-11-30 02:23:23
function fuc_location($ID,$location){
global $table_class;
//if(!$ID){ print_r($location); return $location;}
if($ID){
$sql="select * from $table_class where ID=$ID";
$result=mysql_query($sql) or die(mysql_error());
$row1 = mysql_fetch_array($result);
$location[]=$row1["ID"]."".$row1["class_name"]."";

fuc_location($row1["parentID"],$location); //
}else{
print_r($location);return $location;}

}
请问一下为什么我print_r 输出的值是对的,然后想马值 $location return就是return不出来.谢谢,望高手赐教!!!
...全文
98 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
SysTem128 2007-12-01
  • 打赏
  • 举报
回复

<?php
function fuc_location($ID,$location){
global $table_class;
//if(!$ID){ print_r($location); return $location;}
if($ID){
$sql="select * from $table_class where ID=$ID";
$result=mysql_query($sql) or die(mysql_error());
$row1 = mysql_fetch_array($result);
$location[]=$row1["ID"]."".$row1["class_name"]."";
return fuc_location($row1["parentID"],$location); //
}else{
return $location;
}
}
?>
foolbirdflyfirst 2007-12-01
  • 打赏
  • 举报
回复
应该不要else,否则无法return.
function fuc_location($ID){
global $table_class;
static $location = array();

//if(!$ID){ print_r($location); return $location;}
if($ID){
$sql="select * from $table_class where ID=$ID";
$result=mysql_query($sql) or die(mysql_error());
$row1 = mysql_fetch_array($result);
$location[]=$row1["ID"]."".$row1["class_name"]."";

fuc_location($row1["parentID"]); //
}
return $location;

}
foolbirdflyfirst 2007-12-01
  • 打赏
  • 举报
回复
改写试试:

function fuc_location($ID){
global $table_class;
static $location = array();

//if(!$ID){ print_r($location); return $location;}
if($ID){
$sql="select * from $table_class where ID=$ID";
$result=mysql_query($sql) or die(mysql_error());
$row1 = mysql_fetch_array($result);
$location[]=$row1["ID"]."".$row1["class_name"]."";

fuc_location($row1["parentID"]); //
}else{
print_r($location);return $location;}

}
foolbirdflyfirst 2007-12-01
  • 打赏
  • 举报
回复
lz知不知道什么是局部变量?你应该将$location设为静态变量或者全局变量,这样在整个递归调用中都会应用同一个变量.
suchengfei 2007-11-30
  • 打赏
  • 举报
回复
如果return出来有值我就不用问了,就是不出,但是我用print_r打印值是有的,这回应该明白了吧,不明白本人也没办法,望高人指点!!
suchengfei 2007-11-30
  • 打赏
  • 举报
回复
晕了,这么明白还听不懂,我用return是出不来值的 所以用print_r打印看看是有值的,明白了吗,超级晕晕...
foolbirdflyfirst 2007-11-30
  • 打赏
  • 举报
回复
static $location;
shubin_lin 2007-11-30
  • 打赏
  • 举报
回复
确实不知道楼主在说什么,呵呵,给两个回答,1,return是返回值,而不会打印出来,2,$location好象是数组,如果是数组要打印不可以直接echo $location.
suchengfei 2007-11-30
  • 打赏
  • 举报
回复
??这么清楚还不知所云??? 看最后一句,print_r($location);return $location;} print_r出来的值是有的 但是用return,函数就是没值回去.:(
princekey 2007-11-30
  • 打赏
  • 举报
回复
不知所云

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧