如何使用for循环来引用变量名中的数字?

时间:2013-06-19 05:22:00

标签: php sql variables concatenation operator-keyword

$_SESSION_Job101=mysql_fetch_array(mysql_query("SELECT progress FROM job_101 WHERE username='$_SESSION_User'"));
$_SESSION_Job102=mysql_fetch_array(mysql_query("SELECT progress FROM job_102 WHERE username='$_SESSION_User'"));
$_SESSION_Job103=mysql_fetch_array(mysql_query("SELECT progress FROM job_103 WHERE username='$_SESSION_User'"));
$_SESSION_Job104=mysql_fetch_array(mysql_query("SELECT progress FROM job_104 WHERE username='$_SESSION_User'"));
$_SESSION_Job105=mysql_fetch_array(mysql_query("SELECT progress FROM job_105 WHERE username='$_SESSION_User'"));
$_SESSION_Job106=mysql_fetch_array(mysql_query("SELECT progress FROM job_106 WHERE username='$_SESSION_User'"));
$_SESSION_Job107=mysql_fetch_array(mysql_query("SELECT progress FROM job_107 WHERE username='$_SESSION_User'"));
$_SESSION_Job108=mysql_fetch_array(mysql_query("SELECT progress FROM job_108 WHERE username='$_SESSION_User'")); 
$_SESSION_Job109=mysql_fetch_array(mysql_query("SELECT progress FROM job_109 WHERE username='$_SESSION_User'"));
$_SESSION_Job1010=mysql_fetch_array(mysql_query("SELECT progress FROM job_110 WHERE username='$_SESSION_User'"));

for ($x=1;$x<=10;$x++)
{
$e = '$_SESSION_Job10'.$x;
//if ($e >= 100)
//{ $_SESSION_Job10.$x.['progress'] = 100; }
echo $e;
}

我可以得到$ e变量来返回我想要的变量,但是我希望$ e在if语句中基本上充当该变量,而不仅仅是返回它的名字。当我从if语句中删除注释时,我的页面变为空白而不是回显$ e。我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

使用此

$e = ${'_SESSION_Job10'.$x}; // $e = $_SESSION_Job101; actual variable here

Live Demo

而不是

$e = '$_SESSION_Job10'.$x;   // $e = '$_SESSION_Job101'; // string here

修改

你必须像这样使用数组

${'_SESSION_Job10'.$x}['progress'];

Demo