我有一个脚本,我从数据库中检索多个值并将它们存储在以下变量中:
$Av_1o=99;
$Av_2o=100;
$Av_3o=80;
$Av_4o=70;
$Av_5o=90;
$Av_6o=110;
$Av_7o=120;
$Av_8o=110;
$Av_9o=90;
$Av_10o=88;
$Av_11o=99;
$Av_12o=100;
$Av_13o=101;
然后,根据另一个变量,例如年份,我必须在计数器中设置一个最大值(命名为:$top_counter):
//defining year:
$year=2009;
switch($year){
case 2008:
$top_counter=13;
break;
case 2009:
$top_counter=11;
break;
case 2010:
$top_counter=9;
break;
case 2011:
$top_counter=7;
break;
case 2012:
$top_counter=5;
break;
case 2013:
$top_counter=3;
break;
case 2014:
$top_counter=1;
break;
default:
$top_counter=NULL;
}
最后,我使用了一个for 循环:
for($counter=1;$counter<=$top_counter;$counter++){
$variable_name='Av_'.$counter.'o';
if($$variable_name>=100){
echo '<p style="color: green;">'.$variable_name.' is equal or greater than 100</p>';
}
}
我的问题是如何根据相应的数字($counter)告诉php使用哪个变量(在顶部声明)?
对于这种情况,当年份为 2009 年时,我得到的输出如下:
Av_2o is equal or greater than 100
Av_6o is equal or greater than 100
Av_7o is equal or greater than 100
Av_8o is equal or greater than 100
如何打印变量名?或者确切地知道我当前使用的是哪个 $Av_xo?
谢谢!