使变量在分配给另一个变量php时不发生更改


Make variable not change when assign to another variable php

我在php中遇到了一个问题,比如下面的

$test="测试"

//当我更改下面的代码时,我想在这里做一些事情来保持变量$test的值。

$test="你好词";

print_r($test)

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

您希望$test是常量还是什么?

define("FOO",     "something");

我不知道我是否正确理解你的问题,但也许你想把你的插件放在一个函数中?这样$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