如何使用for循环php对多个字符串的值求和?

如何使用for循环php对多个字符串的值求和?,php,string,for-loop,Php,String,For Loop,如何轻松地使用for循环php对下面字符串中的所有值求和 $modules\u m\u 1结果应为:10 $g_module_row_active_1 = 1; $g_module_row_active_2 = 1; $g_module_row_active_3 = 1; $g_module_row_active_4 = 1; $g_module_row_active_5 = 1; $g_module_row_active_6 = 1; $g_module_row_a

如何轻松地使用for循环php对下面字符串中的所有值求和

$modules\u m\u 1
结果应为:10

  $g_module_row_active_1 = 1;
  $g_module_row_active_2 = 1;
  $g_module_row_active_3 = 1;
  $g_module_row_active_4 = 1;
  $g_module_row_active_5 = 1;
  $g_module_row_active_6 = 1;
  $g_module_row_active_7 = 1;
  $g_module_row_active_8 = 1;
  $g_module_row_active_9 = 1;
  $g_module_row_active_10 = 1;

  $modules_m_1 = $g_module_row_active_1 + $g_module_row_active_2 + $g_module_row_active_3 + $g_module_row_active_4 + $g_module_row_active_5 + $g_module_row_active_6 + $g_module_row_active_7 + $g_module_row_active_8 + $g_module_row_active_9 + $g_module_row_active_10;

您可以使用动态变量来实现这一点,如下所示:

$g_module_row_active_1 = 1;
$g_module_row_active_2 = 1;
$g_module_row_active_3 = 1;
$g_module_row_active_4 = 1;
$g_module_row_active_5 = 1;
$g_module_row_active_6 = 1;
$g_module_row_active_7 = 1;
$g_module_row_active_8 = 1;
$g_module_row_active_9 = 1;
$g_module_row_active_10 = 1;

$modules_m_1 = 0;

for($i=1;$i<=10;$i++)
{

    $modules_m_1 += ${"g_module_row_active_" . $i};

}

echo $modules_m_1;
$g\u模块\u行\u活动\u 1=1;
$g_模块_行_活动_2=1;
$g_模块_行_活动_3=1;
$g_模块_行_活动_4=1;
$g_模块_行_活动_5=1;
$g_模块_行_活动_6=1;
$g_模块_行_活动_7=1;
$g_模块_行_活动_8=1;
$g_模块_行_活动_9=1;
$g\u模块\u行\u活动\u 10=1;
$modules_m_1=0;

对于($i=1;$i,由于它不是完全重复的,所以不标记它,但有一些信息可以帮助您开始回答。您不需要将变量
回送到
,而是要将它们添加到新变量中,并在循环后处理结果。可能的重复