Home > Backend Development > PHP Tutorial > PHP自定义函数如何传值过去

PHP自定义函数如何传值过去

WBOY
Release: 2016-06-06 20:22:53
Original
1065 people have browsed it

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

直接在参数里写好默认值

还有,正如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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template