【问题标题】:How to sum values from multiple strings using for loop php?如何使用for循环php对多个字符串的值求和?
【发布时间】:2019-01-05 15:29:36
【问题描述】:

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

$modules_m_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;

【问题讨论】:

标签: php string for-loop


【解决方案1】:

您可以像这样使用动态变量来实现:

$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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2014-04-02
    相关资源
    最近更新 更多