Here's the code first:
<?php
$test = 'nothing';
function check_test(){
global $test;
echo 'The test is '.$test.'\n';
}
function run($lala){
$test = $lala;
check_test();
}
check_test();
run('Test 2');
run('Test 3');
check_test();
AFAIK in Python it would work, because it searches variables to upper scope, but looks like it works different in php. So here is the question: how can I achieve that behaviour - so function will use first variable occurance and won't start looking from the higher scope-level. In this example I wanted to get output.
The test is nothing
The test is Test 2
The test is Test 3
The test is nothing
But got only
The test is nothing
for 4 times.
Means that very first variable declaration was used. Much appreciate with any suggestions to this!
This is not duplicate, I understand the conception of scope, I'am asking about is it possible to achieve certain behaviour in this snippet.
UPD: I can't use proposed methods because we use pthreads and each function will run in the same time and global variable will be changed every second and that's not what I want. Instead I need that every thread will be using its own "local" global test variable.