<?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?
更新 - 这个问题在下面得到了极好的回答,所以只是为未来的用户重新上限:
<?php
$Trucking_CPA="25.00"; // set price
$Trucking_price="\$Trucking_CPA";
echo ${substr($Trucking_price,1)}; // returns 25.00 instead of $Trucking_CPA
?>
答案 0 :(得分:0)
您可以尝试以下操作:
echo ${substr($Trucking_price,1)};
它会从字符串$
中剪切$Trucking_price
并返回Trucking_CPA
。之后,您可以像${'Trucking_CPA'}
一样调用此变量。