0

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.

user3416803
  • 379
  • 1
  • 2
  • 12
  • You are not actually modifying the global test value in your `run` function. You are changing the value that is visible within that function – Thamilhan May 05 '16 at 06:52
  • 1
    Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – dimlucas May 05 '16 at 06:54
  • @That's what I wanted. So every function will use it's nearest $test variable as global. – user3416803 May 05 '16 at 06:54

3 Answers3

1

You need to use global here also.

function run($lala){
    global $test = $lala;
    check_test();
}

but there is a problem when the last check_test(); function call then you will get he same value of $test as for 3rd one.

Example:

The test is nothing
The test is Test 2
The test is Test 3
The test is Test 3

Suggestion:

So if you really want to get the output like you show the you need to pass a parameter to your check_test() function.

Example:

function check_test($arg= null) {
    global $test;

    $arg= ($arg== null) ? $arg: $test;

    echo "The test is ".$arg."<br/>";
}

function run($par){
    check_test($par);
}

The test is nothing
The test is Test 2
The test is Test 3
The test is nothing
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • Yes, but I don't want to use a parameter, because it ends up in changing a lot of code. So thats why I want to know how can described behaviour can be achieved. – user3416803 May 05 '16 at 07:11
  • there is no other way to do this, only some options: `1. pass parameter 2. using global. 3. reference by value which also need parameter` – Murad Hasan May 05 '16 at 07:39
  • 1
    Thanks, probably will use parameters. – user3416803 May 05 '16 at 07:45
1

In function run you are setting $lala to local parameter, not for global $test = 'nothing'.

I would to like this:

$test = 'nothing';

function check_test($param = null) {
    global $test;

    // if no parameter passed, than use global variable.
    $param = is_null($param) ? $param : $test;

    echo "The test is {$param}\r\n";
}

function run($param){
    check_test($param);
}

check_test();
check_test('Test 2');
check_test('Test 3');
check_test();

Working example

Justinas
  • 41,402
  • 5
  • 66
  • 96
  • this is same like i suggest in my answer. – Murad Hasan May 05 '16 at 07:05
  • The idea is to use different variable values without using parameters or changing global variable. So each function will use "nearest" variable with name $test. – user3416803 May 05 '16 at 07:13
  • @FrayneKonok Sorry, I can't see your answer. And are you telling me that I have copy-pasted your answer? – Justinas May 05 '16 at 08:01
  • @user3416803 What do you mean `nearest variable`? It's either in scope or not. – Justinas May 05 '16 at 08:02
  • no, i did't. How can i say like that?? I just say what i suggest is almost like your answer. My first answer is not like your answer and at the bottom of my first answer i just suggest to OP that concept similar to your answer. Sorry if i do any wrong. – Murad Hasan May 05 '16 at 08:05
  • @Justinas, there's a few scope levels, for example, in Python: http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules. So i thought there's somethin lke this in php. – user3416803 May 05 '16 at 08:10
0

Try below code you will get your desired output here i have change the at last i called run method and in run method i checked if parameter is empty then set "nothing" word in global variable if there is some value in parameter then set that value in global test variable. try below code may it helpful for you.

<?php
$test = 'nothing';

function check_test(){
    global $test;

    echo 'The test is '.$test.'<br/>';

}


function run($lala){

   $GLOBALS['test'] = !empty($lala) ? $lala : 'nothing';
    check_test();
}


check_test();

run('Test 2');
run('Test 3');
run('');
?> 
Denis Bhojvani
  • 817
  • 1
  • 9
  • 18
  • Thanks for suggestion, but global variable must left the same won't be changed by functions, instead of this each function must use nearest variable as global - that's what I'am asking :) – user3416803 May 05 '16 at 07:15