dongxia2030 2012-11-10 03:57
浏览 13
已采纳

获取函数参数值PHP

I have a function with 5 arguments and default site width variable

$site_width ='1000px';
function MyPrintFunction ($divName,$width=false,$html1='',$html2='', $echo = true ){
      ... plenty of code above, no need to post complete function

        $html = '<div id="'.$divName.'">';
        $html .= '</div>';
        if( $echo ){
          echo $html1.$html.$html2;
        }else{
          return $html1.$html.$html2;
        }


}

and than I print html like:

MyPrintFunction ('divname1',true);
MyPrintFunction ('divname2');
MyPrintFunction ('divname3');

I need to check if any of these functions has set the second argument to true, and if yes , I need first argument name.

This way I can add the argument name to css snippet ( will be injected in head later ) and set width for it

#ARrument1NameSHouldGoHereAsVariable{
   width:$site_width;
}

I hope I explained this right. Sorry if I did not. Any help is appreciated. Thank you!

  • 写回答

2条回答 默认 最新

  • doubaoxue5788 2012-11-10 04:36
    关注

    Noo no no, you absolutely can have optional arguments. It's a bit messy of a situation, but totally possible.

    echo MyPrintFunction('dafsd',435);
    function MyPrintFunction ($divName,$width=2342){
        return $divName . $width;
    }
    

    The reason it is discouraged is because... Let's suppose you want to set a height, but not a width to 435

    echo MyPrintFunction('dafsd',435);
    function MyPrintFunction ($divName,$width=2222, $height=1111){
        return $divName . $width . $height;
    }
    

    Optional arguments should generally not be used, and if they are to place them at the end in an array

    Please oh please let this be what you are seeking...

    function MyPrintFunction ($divName,$width=false){
        $newObj = array();
        $newObj['divName'] = $divName;
        $newObj['width'] = $width;
        return $newObj;
    }
    function hasWidth($newObj) {
        if ($newObj['width']) {
            return $newObj['width'];
        }
        else {
            return 'no width!'; 
        }
    }
    $tempStore1 = MyPrintFunction('dafsd',123);
    $tempStore2 = MyPrintFunction('dafsd');
    echo hasWidth($tempStore1);
    echo hasWidth($tempStore2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路