I have a functions.php file, in which there is a function that parses the json string, decodes it and returns an array as an argument to the function return $response; . I connect this file to index.php with require('php/functions.php'); at the beginning of the file, and then I try to infer an element of the array:

 echo $response['name'] . ',+' . $response['country_name']; 

But I get the error Notice: Undefined variable: response . Explain why this is happening? After all, if I connect a file with some functions to an index file, does this imply that the functions should be loaded into the main file? And how do I get access to a variable in a function?

    1 answer 1

    You have a function that can take (or not take) something at the input and give something at the output. The fact that the value of a variable is given inside the function does not mean that the variable will be accessible from the outside. In addition, the function must be called to get the result. And assign it to some variable.

    Something like this happens:

     public function foo($a) { $response = 'json чего-то там:' . $a; return $response; } //а теперь вы пытаетесь использовать $response и вылетает ошибка. //Т.к. область видимости переменной $response только внутри функции foo() //нужно примерно так: $myResponse = foo('hoho'); // $myResponce будет равен 'json чего-то там:hoho'