Declaring a global array

后端 未结 3 1149
清酒与你
清酒与你 2021-01-03 23:09

Hi. I recently learned PHP and am trying to declare a global array so I can access inside a function. But I seem to be missing something because I get the error \'Undefined

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 23:38

    As about everybody suggested, you should pass your array as function parameter. Using globals is a bad practice in major cases.

                function operatii($second_array, $v) {
                    $var1 = $second_array[count($second_array)-1];
                    $var2 = $second_array[count($second_array)-2];
                    $rez = null;
                    echo $var1 . $var2 . "este?";
                }
    
                $second_array = array();
    
                for ($i = 0; $i < count($a); $i++) {
                    if ($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ) {
                        operatii($second_array, $a[$i]);
                    } else {
                        array_push($second_array, $a[$i]);
                    }
                }
    

提交回复
热议问题