分配给另一个变量php时,使变量不变

时间:2015-03-22 12:08:10

标签: php

我在php中遇到问题,如bellow

$ test ='test'

//我想在这里做一些事情,以便在我改变代码时保持变量$ test的值。

$ test ='hello word';

的print_r($试验)

... print_r($ test)的任何理想结果都是'test'。 感谢。

2 个答案:

答案 0 :(得分:0)

你希望你的$ test是常数还是什么?

define("FOO",     "something");

答案 1 :(得分:0)

我不知道我是否正确理解了你的问题,但也许你想把你的插件放在一个函数中?这样$ text变量将受其范围的保护:

$test = 'test';
echo "Test varable is initially: $test", PHP_EOL;

// I want to do something here to keep value of variable $test
// when I change code bellow.
$f = function () use ($test) {
    echo "Test in start of function: $test", PHP_EOL;
    $test = 'hello word'; // Plugin does something to $test
    echo "Test in function has changed to: $test", PHP_EOL;
};
$f(); // Call your function above with the plugin

echo "Test variable when returning from function is still: $test", PHP_EOL;

输出:

Test varable is initially: test
Test in start of function: test
Test in function has changed to: hello word
Test variable when returning from function is still: test