Initially, it somehow worked, but I had to redo the script and now it is many times more complicated, in addition, the transfer of the array to the function is incorrect and I do not receive data in the function.

$cx1 = -1.2; $cx2 = 0; $delta = 0.5; function f_compare($x1, $x2, $type) { global $x1, $x2; print_r ($x1); print_r ($x2); return true; } //Исследующий поиск function cycle($cx1, $cx2, $delta, $type) { global $cx1, $cx2, $delta; $x = array(); $x2 = array(); if ($type==1) {$x[0]=$cx1; $x2[0]=$cx2;} print_r ($x); print_r ($x2); echo f_compare($x,$x2,$type); } cycle($cx1, $cx2, $delta, 1); 

The output in the cycle function will work, but the output in the f_compare function f_compare not, why is that?

    2 answers 2

    Remove global in functions, you overwrite your arguments with them, if the first function works because your arguments are defined in the global scope, then the f_compare () function does not find $ x1 and $ x2, since they are in the visibility of the cycle () function.

     <?php $cx1 = -1.2; $cx2 = 0; $delta = 0.5; function f_compare($x1, $x2, $type) { print_r ($x1); print_r ($x2); return true; } //Исследующий поиск function cycle($cx1, $cx2, $delta, $type) { $x = array(); $x2 = array(); if ($type==1) {$x[0]=$cx1; $x2[0]=$cx2;} print_r ($x); print_r ($x2); echo f_compare($x,$x2,$type); } cycle($cx1, $cx2, $delta, 1); 
    • Oh, you're right. It was just initially strange at all, it was necessary to transfer an array and specify it globally, without one of them, the function for some reason did not receive data at all, so everything is much more logical. Thank you. - Telion

    You too smart, chef.

     <?php $cx1 = -1.2; $cx2 = 0; $delta = 0.5; function f_compare($x1, $x2, $type) { //global $x1, $x2; print '<br>f_compare<br>'; print_r ($x1); print_r ($x2); return true; } //Исследующий поиск function cycle($cx1, $cx2, $delta, $type) { global $cx1, $cx2, $delta; $x = array(); $x2 = array(); if ($type==1) {$x[0]=$cx1; $x2[0]=$cx2;} print '<br>cycle<br>'; print_r ($x); print_r ($x2); echo f_compare($x,$x2,$type); } cycle($cx1, $cx2, $delta, 1);