Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何对字符串使用$符号使其成为变量?_Php - Fatal编程技术网

Php 如何对字符串使用$符号使其成为变量?

Php 如何对字符串使用$符号使其成为变量?,php,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 sto

我制作了一个小的静态测试脚本,只是为了解释我想做什么

<?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
  }
$result=mysql\u查询(“从石头中选择字体”);
while($row=mysql\u fetch\u数组($result))
{
echo$x[$row['font'].“
”;//$row['font']的值为“测试”和“演示” }
$vars=get_defined_vars();
$result=mysql_查询(“从石头中选择字体”);
while($row=mysql\u fetch\u数组($result))
{
$var=$vars[$row['font']];
echo$x[$var]。“
”; } //只是为了好玩:) //仅:|
$vars=get_defined_vars();
$result=mysql_查询(“从石头中选择字体”);
while($row=mysql\u fetch\u数组($result))
{
$var=$vars[$row['font']];
echo$x[$var]。“
”; } //只是为了好玩:) //仅:|
请不要使用mysql。改用mysqli或PDO你试过了吗?它不管用吗?嗯,它应该管用。如果
$test
是字符串
'hello'
并且
$row['font']
是字符串
'hello'
那么
$test====$row['font']
$x[$test]==$x[$row['font']]]
请不要使用mysql。改用mysqli或PDO你试过了吗?它不管用吗?嗯,它应该管用。如果
$test
是字符串
'hello'
并且
$row['font']
是字符串
'hello'
那么
$test====$row['font']
$x[$test]==$x[$row['font']]
。我们也可以使用echo$x[$$row['font']]。也可以使用echo$x[$$row['font']。
$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 :|