如何将$符号用于字符串以使其成为变量?

时间:2012-12-01 11:26:53

标签: php

我已经制作了一个小的静态测试脚本来解释我想要做什么。

<?php
$test = "hello";
$demo = "php";
$x = array("hello"=>"world","php"=>"script");
echo $x[$test]."<br />";
echo $x[$demo];
?>

输出:世界         脚本

当值来自数据库时,我怎样才能动态获得相同的输出。

$result = mysql_query("SELECT font FROM stone");

while($row = mysql_fetch_array($result))
  {
  echo $x[$row['font']]."<br/>";// values of $row['font'] are 'test' and demo
  }

1 个答案:

答案 0 :(得分:1)

$vars=get_defined_vars();
$result = mysql_query("SELECT font FROM stone");
while($row = mysql_fetch_array($result))
{
  $var=$vars[$row['font']];
  echo $x[$var]."<br/>";
}
//just for funny :)
//only :|