Similar questions exists - I've tried already some solutions from StackOverflow and Google but still no luck - I can't catch a right syntax or right understanding - so let me repeat my question with more details for clarity.
I have a PHP (PHP 7.4) class file what is a part of MVC with two functions inside- and I try to pass a variable from one function to another using global variable.
My question is - please, check my code below - if you see something wrong then pls give an answer how it should be (because if code is correct with syntax and logic - it means the problem is not inside this file but may be somewhere outside) - but I need other opinions to be sure.
// I use control files for quick tests because if OK I see output immediately
public function indexAction()
{
global $myArr;
if ($this->req->get('lang')) {
$lang = $this->req->get('lang');
$myArr[] = $lang;
file_put_contents('/var/www/html/app/controllers/control_indexaction_lang.php', $lang); // output OK - en de etc...
file_put_contents('/var/www/html/app/controllers/control_indexaction_myarr.php', $myArr); // output OK - en de etc...
// with every change of layout control file changes accordingly to en de it etc
// so $lang variable actually works here
} else {
$lang = 'en';
}
}
public function searchAction()
{
global $myArr;
file_put_contents('/var/www/html/app/controllers/control_searchaction_lang.php', $lang); // no output
file_put_contents('/var/www/html/app/controllers/control_searchaction_myarr.php', $myArr); // no output
// other code of function works OK
}