6
\$\begingroup\$

I've been reading about loops this morning, to try to improve this snippet of code:

$deliveryoutcode1 = array();
$deliveryoutcode1 = explode(",",$rows['deliveryoutcode1']);
$deliveryoutcode2 = array();
$deliveryoutcode2 = explode(",",$rows['deliveryoutcode2']);
$deliveryoutcode3 = array();
$deliveryoutcode3 = explode(",",$rows['deliveryoutcode3']);

I started to read about loops this morning and I came up with this:

$i=1;
while ($i <= 3) { // Output values from 0 to 3
    $deliveryoutcode[$i] = array();
    $deliveryoutcode[$i] = explode(",",$rows['deliveryoutcode' . $i . '']);
    $i++;
}

This works great, but I'm wondering if there is a better way to do this. It appears as though I've come up with the best way but as this is my first attempt at loops, I better ask the experts.

\$\endgroup\$
2
\$\begingroup\$
  • you don't need the declaration $deliveryoutcode1 = array();
  • a for loop is more readable (and it fulfills exactly that purpose)
  • $rows['deliveryoutcode' . $i . ''] could be $rows['deliveryoutcode' . $i] or (more compact) $rows["deliveryoutcode$i"]

--

for($i = 1; $i <= 3; $i++) { 
    $deliveryoutcode[$i] = explode(",", $rows["deliveryoutcode$i"]);
}
\$\endgroup\$
  • \$\begingroup\$ I've undeleted this answer for you. It is a fine answer, and you were the first to post a proper answer. (For the record, I delete answers disguised as comments, precisely because they discourage other users from posting proper answers.) \$\endgroup\$ – 200_success Feb 9 '15 at 15:31
  • \$\begingroup\$ thank you for this solution but do I need the $i++ at the end? It causes errors my end. \$\endgroup\$ – luke_mclachlan Feb 9 '15 at 17:59
  • \$\begingroup\$ Sorry. My mistake :) \$\endgroup\$ – Luca Rainone Feb 9 '15 at 19:32
5
\$\begingroup\$

Several things to mention:

1. Use a for loop.

Semantically speaking, a for loop is way more suited in your case than a while loop. for usually means "repeat this for a specified number of steps" while while means "repeat this as long as a given condition is true". Given, you can ALWAYS replace a for with a while from a technical point of view and exactly this is the reason why you need to decide which one to use based on semantics

2. No need to use explicit array initialization.

When you assign the result of explode to your array object the interpretor will take care of the object type. There is no reason in this case to make it explicit so it is better to delete that line.

3. Naming

Not sure if this is part of the exercise or not, but consider using proper names for your objects. What do you explode? What does the array contains? Is a list of houses? Are those candies? Users? deliveryoutcode does not have too much meaning.

If you really don't have any real objects to work on, invent some and use those. It creates good habits for your programming journey.

\$\endgroup\$
  • \$\begingroup\$ i'm a point away from voting up your answer so thank you for your input \$\endgroup\$ – luke_mclachlan Feb 9 '15 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.