dongyuan7981 2013-02-05 11:32
浏览 13
已采纳

PHP:在将变量名称传递给函数时是否需要更改变量名称?

I have a body function and a function called within the first one.

As can be seen below I don't change the parameters name while using in the second function.

Is it necessary to change the params names for use inside _display_bar();? What are the side effects if I don't?

    function main_func($form, &$form_state, $key, $code) {
    $output = '';
    ...
    $output .= _display_navbar($trans, $status_names);

    return $output
    }

    function _display_navbar($trans, $status_names) {
     $trans = 'bla';
     $status_names = 'another bla';
   $bar = $trans . ':' .$status_names;
     return $bar;
    };
  • 写回答

4条回答 默认 最新

  • dongman2721 2013-02-05 11:41
    关注

    It actually does not matter. But you better should not have the same names - it is confusing. Let me give you an example. $s will have 3 after the first function call to sum; 7 after the second function call to sum. The parameters did not have the same name as the function parameter names.

    To answer your question fully - there are absolutely no side effects.

    function main()
    {
        $a = 1;
        $b = 2;
        $s = sum($a, $b);
    
        $d = 3;
        $e = 4;
        $s = sum($d, $e);
    }
    
    function sum($first, $second) 
    {
        $ret = $first + $second;
        return $ret;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题