我有以下的PHP摘錄代碼:有效的,和非冗餘PHP代碼
foreach($afb_replacements as $afb_to_replace => $afb_replacement) {
$sender_subject = str_replace($afb_to_replace, $afb_replacement, $sender_subject);
$ar_subject = str_replace($afb_to_replace, $afb_replacement, $ar_subject);
$final_message = str_replace($afb_to_replace, $afb_replacement, $final_message);
$final_message_text = str_replace($afb_to_replace, $afb_replacement, $final_message_text);
$ar_message = str_replace($afb_to_replace, $afb_replacement, $ar_message);
$ar_message_text = str_replace($afb_to_replace, $afb_replacement, $ar_message_text);
}
所有6個變量以同樣的方式被替換(同文同在$ afb_to_replace所有變量相同的替換替換和$ afb_replacement)。
我想知道的是:
這又如何更有效地寫的?也許在一行代碼中。我相信有更好的辦法,因爲這是多餘的代碼,但目前還沒有其他解決方案進入我的腦海。有任何想法嗎?
我對你的方法很好奇!
如果你使用'compact()'和'extract()',它會保存一些變量名的輸入。 – BoltClock 2011-05-15 19:47:47