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 :|