我知道类似的问题都来了几次,但我似乎无法找到一个会帮助我想做的事情。
I realise similar questions have come up a few times, but I can't seem to find one that will help with what I am trying to do.
我们有一个config.php文件基本上是变量列表,用户组,以及它们的值,例如:
We have a config.php file which is basically a list of variables that a user sets, along with their values, eg:
$OUR_EMAIL_ADDRESS = 'test@test.com';
$OUR_WEBSITE = 'www.test.com';
就像所有我们想通过JavaScript传递PHP的值的其他用户。
Just like all the other users we want to transfer a PHP value via Javascript.
我们已经这样做了的方式,通过AJAX - 但它不是那么灵活,因为我们想为每个变量需要自定义脚本/ PHP页面:
We have a way of doing this already, via ajax - but it is not as flexible as we would like as each variable needs a custom script / php page:
第1部分 - 的JavaScript(jQuery的)
$.get("/getConfig_email.php", function(email){
alert(email);
});
第2部分:getConfig_email.php
<?php
echo $OUR_EMAIL_ADDRESS_VALUE;
?>
的问题:
有没有办法PHP可以使用一个字符串作为一个变量名? IE浏览器。我想要做的是这样的:
Is there a way that PHP can use a string as a variable name? ie. What I want to do is this:
第1部分 - 的JavaScript(jQuery的)
$.get("/getConfig.php?name=OUR_EMAIL_ADDRESS_VALUE", function(email){
alert(email);
});
第2部分:getConfig.php
<?php
$var_name = $_GET['name'];
//some magic here to turn the name of the variable
//in to the actual variable - a bit like a reverse extract
echo $var_name.value;
?>
你怎么看?这是可能以某种方式 - 我希望也只是一个PHP命令,可以为我做到这一点!
What do you think? Is this possible somehow - I'm hoping there is just a php command that can do this for me!
在预先感谢。
是的,这是可以通过的 可变变量
:
Yes, it is possible via variable variables
:
echo $$var_name;
然而,这是不明智的,因为你正在服用的用户输入,并可能用户可以得到内部变量,都应该被隐藏的值。
However, this is unwise since you are taking user input and possibly the user can get values of internal variables that are supposed to be hidden.