php 变量范围问题
php variable scope quetion
首先是代码:
<?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 在 Python 中它会工作,因为它搜索变量到上层范围,但看起来它在 php 中工作不同。所以这里的问题是:我怎样才能实现这种行为 - 所以函数将使用第一个变量出现并且不会从更高的范围级别开始寻找。在这个例子中,我想得到输出。
The test is nothing
The test is Test 2
The test is Test 3
The test is nothing
但是只得到了
The test is nothing
4次。
表示使用了第一个变量声明。非常感谢对此的任何建议!
这不是重复的,我理解范围的概念,我想问的是是否有可能在此代码段中实现某些行为。
UPD:我不能使用建议的方法,因为我们使用 pthreads 并且每个函数将同时 运行 并且全局变量将每秒更改一次,这不是我想要的。相反,我需要每个线程都使用自己的 "local" 全局测试变量。
你也需要在这里使用 global
。
function run($lala){
global $test = $lala;
check_test();
}
但是当最后一个 check_test();
函数调用时出现问题然后你将得到与第三个函数相同的 $test
值。
示例:
The test is nothing
The test is Test 2
The test is Test 3
The test is Test 3
建议:
因此,如果您真的想获得像您显示的那样的输出,则需要将参数传递给 check_test()
函数。
示例:
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
在函数 run
中,您将 $lala
设置为局部参数,而不是全局参数 $test = 'nothing'
。
我喜欢这个:
$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();
试试下面的代码,你会在这里得到你想要的输出我改变了最后我调用的运行方法,在运行方法中我检查了参数是否为空然后设置"nothing"全局变量中的单词如果参数中有某个值,则在全局测试变量中设置该值。试试下面的代码可能对你有帮助。
<?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('');
?>
首先是代码:
<?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 在 Python 中它会工作,因为它搜索变量到上层范围,但看起来它在 php 中工作不同。所以这里的问题是:我怎样才能实现这种行为 - 所以函数将使用第一个变量出现并且不会从更高的范围级别开始寻找。在这个例子中,我想得到输出。
The test is nothing
The test is Test 2
The test is Test 3
The test is nothing
但是只得到了
The test is nothing
4次。
表示使用了第一个变量声明。非常感谢对此的任何建议!
这不是重复的,我理解范围的概念,我想问的是是否有可能在此代码段中实现某些行为。
UPD:我不能使用建议的方法,因为我们使用 pthreads 并且每个函数将同时 运行 并且全局变量将每秒更改一次,这不是我想要的。相反,我需要每个线程都使用自己的 "local" 全局测试变量。
你也需要在这里使用 global
。
function run($lala){
global $test = $lala;
check_test();
}
但是当最后一个 check_test();
函数调用时出现问题然后你将得到与第三个函数相同的 $test
值。
示例:
The test is nothing
The test is Test 2
The test is Test 3
The test is Test 3
建议:
因此,如果您真的想获得像您显示的那样的输出,则需要将参数传递给 check_test()
函数。
示例:
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
在函数 run
中,您将 $lala
设置为局部参数,而不是全局参数 $test = 'nothing'
。
我喜欢这个:
$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();
试试下面的代码,你会在这里得到你想要的输出我改变了最后我调用的运行方法,在运行方法中我检查了参数是否为空然后设置"nothing"全局变量中的单词如果参数中有某个值,则在全局测试变量中设置该值。试试下面的代码可能对你有帮助。
<?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('');
?>