dongza1708 2011-06-21 23:41
浏览 11
已采纳

无法理解一种行为

Why is it that even though my code is like this:

if ( ! function_exists('get_values')){
    global $str;
    function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal){
            $totalPares = $tpar;
            $ventasPiso = $vpiso;
            $totalComisiones = $tcomi;
            $totalGastos = $tgas;
            $totalTotal = $ttotal;
            $str = $totalPares . "," . $ventasPiso . "," . $totalComisiones . "," . $totalGastos . "," . $totalTotal;
            return $str;
    }

    function getEm(){
        return $str;
    }
}

I can't override the value of $str. It always prints ¨a¨ if I try to echo it, and inside the "getEm" function it says it's an undefined variable.

This is the helper file in the codeigniter framework.

EDIT

which still gives me this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: str

Filename: helpers/helper_common_functions_helper.php

Line Number: 15

  • 写回答

3条回答 默认 最新

  • douchuitang0642 2011-06-21 23:43
    关注
    global $str;
    

    You need to research variable scoping. Functions have their own scope unless you have global $variable at the start to access the variables outside that function. Please also note, using global variables is a "nono" and should be avoided at all cost.

    function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal){
            global $str;
            $totalPares = $tpar;
            $ventasPiso = $vpiso;
            $totalComisiones = $tcomi;
            $totalGastos = $tgas;
            $totalTotal = $ttotal;
            $str = $totalPares . "," . $ventasPiso . "," . $totalComisiones . "," . $totalGastos . "," . $totalTotal;
            return $str;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀