Maison >développement back-end >tutoriel php >PHP自定义函数如何传值过去
<code>function get_lm($need_zhlm){ $top_nav_name=array(array('url'=>u('tradelist','set'),'name'=>'淘宝联盟设置'),array('url'=>u('mall','set'),'name'=>'综合联盟'),array('url'=>u('paipai_order','set'),'name'=>'拍拍易推广设置')); $zhlm=$webset['zhlm']; $need_zhlm=0; if($zhlm['open']==1){ $need_zhlm=1; $top_nav_name=array_merge($top_nav_name,array(array('url'=>u('mall','duomai_set'),'name'=>'多麦联盟'),array('url'=>u('mall','weiyi_set'),'name'=>'唯一联盟'),array('url'=>u('mall','wujiumiao_set'),'name'=>'59秒联盟'),array('url'=>u('mall','yiqifa_set'),'name'=>'亿起发联盟'),array('url'=>u('mall','linktech_set'),'name'=>'领科特联盟'),array('url'=>u('mall','chanet_set'),'name'=>'成果联盟'),array('url'=>u('mall','yqh_set'),'name'=>'一起惠'),array('url'=>u('bijia','set'),'name'=>'全网搜索'))); } return $top_nav_name; }</code>
zhlm为数据库字段,我希望$need_zhlm在数据库zhlm字段为0时赋值为0,zhlm字段为1时赋值为1,请帮我看看这样写是否正确
<code>function get_lm($need_zhlm){ $top_nav_name=array(array('url'=>u('tradelist','set'),'name'=>'淘宝联盟设置'),array('url'=>u('mall','set'),'name'=>'综合联盟'),array('url'=>u('paipai_order','set'),'name'=>'拍拍易推广设置')); $zhlm=$webset['zhlm']; $need_zhlm=0; if($zhlm['open']==1){ $need_zhlm=1; $top_nav_name=array_merge($top_nav_name,array(array('url'=>u('mall','duomai_set'),'name'=>'多麦联盟'),array('url'=>u('mall','weiyi_set'),'name'=>'唯一联盟'),array('url'=>u('mall','wujiumiao_set'),'name'=>'59秒联盟'),array('url'=>u('mall','yiqifa_set'),'name'=>'亿起发联盟'),array('url'=>u('mall','linktech_set'),'name'=>'领科特联盟'),array('url'=>u('mall','chanet_set'),'name'=>'成果联盟'),array('url'=>u('mall','yqh_set'),'name'=>'一起惠'),array('url'=>u('bijia','set'),'name'=>'全网搜索'))); } return $top_nav_name; }</code>
zhlm为数据库字段,我希望$need_zhlm在数据库zhlm字段为0时赋值为0,zhlm字段为1时赋值为1,请帮我看看这样写是否正确
$webset 是哪里来的?最好依照SOLID 原则啦,貌似你的代码隔离性不好啊
理论上你那样写是没问题的
但既然是作为一个参数,我更愿意这样写
<code> function get_lm($need_zhlm = 0) { $top_nav_name = array(array('url' => u('tradelist', 'set'), 'name' => '淘宝联盟设置'), array('url' => u('mall', 'set'), 'name' => '综合联盟'), array('url' => u('paipai_order', 'set'), 'name' => '拍拍易推广设置')); $zhlm = $webset['zhlm']; if ($zhlm['open'] == 1) { $need_zhlm = 1; $top_nav_name = array_merge($top_nav_name, array(array('url' => u('mall', 'duomai_set'), 'name' => '多麦联盟'), array('url' => u('mall', 'weiyi_set'), 'name' => '唯一联盟'), array('url' => u('mall', 'wujiumiao_set'), 'name' => '59秒联盟'), array('url' => u('mall', 'yiqifa_set'), 'name' => '亿起发联盟'), array('url' => u('mall', 'linktech_set'), 'name' => '领科特联盟'), array('url' => u('mall', 'chanet_set'), 'name' => '成果联盟'), array('url' => u('mall', 'yqh_set'), 'name' => '一起惠'), array('url' => u('bijia', 'set'), 'name' => '全网搜索'))); } return $top_nav_name; }</code>
直接在参数里写好默认值
还有,正如jp928所说,$webset这个变量从哪里来的,难道是个在函数外部的全局变量?
<code>function get_lm($need_zhlm=0){ $top_nav_name=array(array('url'=>u('tradelist','set'),'name'=>'淘宝联盟设置'),array('url'=>u('mall','set'),'name'=>'综合联盟'),array('url'=>u('paipai_order','set'),'name'=>'拍拍易推广设置')); $zhlm=$webset['zhlm']; $need_zhlm=0; if($zhlm['open']==1){ $need_zhlm=$need_zhlm; //我希望$need_zhlm在数据库zhlm字段为0时赋值为0,zhlm字段为1时赋值为1,你这样不就行了???直接赋值 $top_nav_name=array_merge($top_nav_name,array(array('url'=>u('mall','duomai_set'),'name'=>'多麦联盟'),array('url'=>u('mall','weiyi_set'),'name'=>'唯一联盟'),array('url'=>u('mall','wujiumiao_set'),'name'=>'59秒联盟'),array('url'=>u('mall','yiqifa_set'),'name'=>'亿起发联盟'),array('url'=>u('mall','linktech_set'),'name'=>'领科特联盟'),array('url'=>u('mall','chanet_set'),'name'=>'成果联盟'),array('url'=>u('mall','yqh_set'),'name'=>'一起惠'),array('url'=>u('bijia','set'),'name'=>'全网搜索'))); } return $top_nav_name; }</code>