PHP-动态访问变量

PHP-动态访问变量,php,Php,在PHP中,是否有一种方法可以根据另一个因素更改正在更改的变量。例如: $str1 = "The string is 1"; $str2 = "The string is 2"; $str3 = "The string is 3"; $X = 3; echo $strX; >> "The string is 3." 编辑:谢谢,这正是我需要的:)我想你在找我 $str1 = "The string is 1"; $str2 = "The string is 2"; $

在PHP中,是否有一种方法可以根据另一个因素更改正在更改的变量。例如:

$str1 = "The string is 1";
$str2 = "The string is 2";
$str3 = "The string is 3";
$X = 3;
echo $strX;

>> "The string is 3."

编辑:谢谢,这正是我需要的:)

我想你在找我

  $str1 = "The string is 1";
  $str2 = "The string is 2";
  $str3 = "The string is 3";
  $X = "str2";
  echo ${$X};
你也可以使用

   $str1 = "The string is 1";
   $str2 = "The string is 2";
   $str3 = "The string is 3";
   $X = "2";
   echo ${"str".$X};


在这里阅读更多信息:
echo${“str”。$X}很少的解释将有助于未来的访问者。
<?php
$str1 = "The string is 1";
$str2 = "The string is 2";
$str3 = "The string is 3";
$X = 3;
echo ${"str".$X};
?>