K
K
kostyaslam2016-12-28 10:55:38
PHP
kostyaslam, 2016-12-28 10:55:38

Why does the function not see the global variable?

Snippet code for modx revo in PHP.

$a = 1; /* глобальная область видимости */ 

function Test()
{ 
global $a; /* Объявляем переменную $a глобальной */
echo $a; /* ссылка на переменную локальной области видимости */ 
} 

Test();

In fact, the number 1 should be displayed. But nothing is displayed. Generally empty.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Zaitsev, 2016-12-28
@dim_s

Most likely, the snippet does not have a global context, you need to write global there too:

global $a;
$a = 1;

.... и т.д.

Y
yuras666, 2016-12-28
@yuras666

Most likely "snipet" is not in the global scope. And if you are already working with global variables, set them explicitly.

$GLOBALS['a'] = 1;

function Test()
{
    echo $GLOBALS['a']; //1
}

Test();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question