RE: [PHP] What's wrong with this code please?

From: Date: Tue, 24 Feb 2004 18:24:21 +0000
Subject: RE: [PHP] What's wrong with this code please?
Groups: php.general 
Request: Send a blank email to php-general+get-178749@lists.php.net to get a copy of this message
Donpro <mailto:donpro@lclnav.com> on Tuesday, February 24, 2004 9:52 AM said: > $emails = > array("email1@domain.com","email2@domain.com",email3@domain.com); > $addresses = explode(",",$emails); for ($i=0; $i < count($addresses); > $i++) { echo $i . ': ' . $addresses[$i] . '<br>'; > if ($i == count($addresses) - 1) > $form['recipient'] .= $addresses[$i]; > else > $form['recipient'] .= $addresses[$i] . ','; > } > echo 'Recipient = ' . $form['recipient'] . '<br>'; why are you exploding an array? $emails is already an array and can already be accessed via index i.e. $emails[0..n]; and similar to what adam metioned, i don't even know what would happen when you explode an array so your unexpected results are, well, expected. ;) $emails = array("email1@domain.com","email2@domain.com",email3@domain.com); $emails_cnt = count($addresses); for ($i = 0; $i < $emails_cnt; $i++) { echo "$i:{$emails[$i]}<br>"; if ($i == ($emails_cnt - 1)) { $form['recipient'] .= $emails[$i]; } else { $form['recipient'] .= $emails[$i] . ','; } } echo "Recipient = {$form['recipient']}<br>"; maybe that will work? (untested) chris. p.s. from now on don't use the count() function within loops (including the loop declaration) because it slows things way down (when you count the same variable over and over). instead do like what i did, count it ahead of time and then just reference the answer.

« previous php.general (#178749) next »