我已经对这件事困惑了一段时间了,我希望有人有个主意。下面是代码:
<?php
$Trucking_CPA="25.00"; // set price
$Trucking_price="\$Trucking_CPA"; // I know, that slash is the problem - but I need it there for other purposes in the program.
echo $Trucking_CPA."<br />"; // prints $25.00 - that's fine and expected
echo $Trucking_price."<br />"; // prints $Trucking_CPA (with no slash for some reason...)
echo $$Trucking_price."<br />"; // With double $ - Gives error: Notice: Undefined variable: $Trucking_CPA - why?
$anothertry1 = ${$Trucking_price};
echo $anothertry1."<br />"; // With double $ in brackets - Gives error: Notice: Undefined variable: $Trucking_CPA - why?
$anothertry2 = ${stripslashes($Trucking_price)};
echo $anothertry2."<br />"; // With double $ in brackets and stripslashes - Gives error: Notice: Undefined variable: $Trucking_CPA - why?
?>我需要有那个斜线,这样它就不会在其他地方转换,因为逻辑正在检查它,在这种情况下,它不能等于变量的值。
有谁知道我如何通过调用$Trucking_price来让PHP给我25.00呢?
Update --这个问题在下面得到了很好的回答,因此,为了给将来的用户重新设置上限,我们会遇到这样的问题:
<?php
$Trucking_CPA="25.00"; // set price
$Trucking_price="\$Trucking_CPA";
echo ${substr($Trucking_price,1)}; // returns 25.00 instead of $Trucking_CPA
?>发布于 2014-05-10 05:32:48
您可以尝试以下方法:
echo ${substr($Trucking_price,1)};它将从字符串$中删除$Trucking_price,并返回Trucking_CPA.After,您可以像${'Trucking_CPA'}一样调用这个变量。
https://stackoverflow.com/questions/23577434
复制相似问题