Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables_Slash_Stripslashes - Fatal编程技术网

PHP难题:当变量的值包含斜杠时查找变量的值

PHP难题:当变量的值包含斜杠时查找变量的值,php,variables,slash,stripslashes,Php,Variables,Slash,Stripslashes,我对这个问题困惑了很长一段时间,我希望有人能有个想法。代码如下: <?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 />"; // print

我对这个问题困惑了很长一段时间,我希望有人能有个想法。代码如下:

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

 ?>
我需要把斜杠放在那里,这样它就不会在其他地方转换,因为逻辑正在检查它,在这种情况下它不能等于变量的值

有没有人知道我怎么能让PHP打电话给$Trucking_price给我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
 ?>

您可以尝试以下操作:

echo ${substr($Trucking_price,1)};

它将从字符串$Trucking\u price中剪切$并返回Trucking\u CPA。之后,您可以像${'Trucking\u CPA'}一样调用此变量。

一个反斜杠将转义下一个字符。您需要避开反斜杠本身。你可以试试这个:$Trucking\u price=\\\$Trucking\u CPA;仔细阅读字符串转义和变量-实际上该字符串中没有反斜杠。