在 PHP 中设置多个具有相似名称和值的变量的最快方法
Quickest way to set many variables with similar names and values in PHP
我想设置 26 个 "counters",每个字母对应一个。它们具有相似的名称,并且都以 0 值开头。必须有一种更快的方法来执行此操作。这是代码:
$a_count = 0;
$b_count = 0;
$c_count = 0;
$d_count = 0;
$e_count = 0;
$f_count = 0;
$g_count = 0;
$h_count = 0;
$a_count = 0;
$i_count = 0;
$j_count = 0;
$k_count = 0;
$l_count = 0;
$m_count = 0;
$n_count = 0;
$o_count = 0;
$p_count = 0;
$q_count = 0;
$r_count = 0;
$s_count = 0;
$t_count = 0;
$u_count = 0;
$v_count = 0;
$w_count = 0;
$x_count = 0;
$y_count = 0;
$z_count = 0;
如何在不占用26行的情况下做到这一点?
改用数组。它将使您的代码在 运行.
中变得更简单 lot
$counts = array_fill_keys(range('a', 'z'), 0);
我想设置 26 个 "counters",每个字母对应一个。它们具有相似的名称,并且都以 0 值开头。必须有一种更快的方法来执行此操作。这是代码:
$a_count = 0;
$b_count = 0;
$c_count = 0;
$d_count = 0;
$e_count = 0;
$f_count = 0;
$g_count = 0;
$h_count = 0;
$a_count = 0;
$i_count = 0;
$j_count = 0;
$k_count = 0;
$l_count = 0;
$m_count = 0;
$n_count = 0;
$o_count = 0;
$p_count = 0;
$q_count = 0;
$r_count = 0;
$s_count = 0;
$t_count = 0;
$u_count = 0;
$v_count = 0;
$w_count = 0;
$x_count = 0;
$y_count = 0;
$z_count = 0;
如何在不占用26行的情况下做到这一点?
改用数组。它将使您的代码在 运行.
中变得更简单 lot$counts = array_fill_keys(range('a', 'z'), 0);