better to use for or foreach in this situation?
Moderator: General Moderators
better to use for or foreach in this situation?
I've got a loop that's going to start at $array[10] and then go through the remaining elements. The number of remaining elements is unknown, as it will be determined by user input into a form. I've come up with two ways of doing this (below). I'm wondering if there's any advantage to one over the other.
foreach($array as $key=>$value){
if($key>=10){
statements;
}
}
OR
for($key=10; $key>=10; $key+=1){
if(array_key_exists($key, $array)){
statements;
}
else{
break;
}
}
The foreach method is more succinct. However, it loops through the first ten elements of the array before getting to the elements on which it will perform the statements, whereas the for loop starts right at the eleventh element of the array. Will the foreach method be slower? Or is the difference so slight that it's best to go with the one that looks simpler?
Thanks,
Drew
foreach($array as $key=>$value){
if($key>=10){
statements;
}
}
OR
for($key=10; $key>=10; $key+=1){
if(array_key_exists($key, $array)){
statements;
}
else{
break;
}
}
The foreach method is more succinct. However, it loops through the first ten elements of the array before getting to the elements on which it will perform the statements, whereas the for loop starts right at the eleventh element of the array. Will the foreach method be slower? Or is the difference so slight that it's best to go with the one that looks simpler?
Thanks,
Drew
Re: better to use for or foreach in this situation?
Could use array_slice:
Code: Select all
foreach (array_slice($array, 10) as $key=>$value) {
// statements
}Re: better to use for or foreach in this situation?
Most people who have been programming for a few months are well familiar with both styles. I'd just use the for loop since it clearly states it's intent, that the code starts at the 10th element, loops until it gets to the Nth element, and increments the array index each iterationoboedrew wrote:The foreach method is more succinct.
Re: better to use for or foreach in this situation?
Ah, array_slice... that's a new one to me, but it sure looks like a good solution. I'll try it out.
Thanks,
Drew
Thanks,
Drew
Re: better to use for or foreach in this situation?
If you know that the keys are numeric then a for would be better. But since I have this nasty habit of assuming the worst I used array_slice. Could just as easily use array_values:
Code: Select all
$values = array_values($array);
for ($i = 10; isset($values[$i]); $i++) {
// statements
}- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
Re: better to use for or foreach in this situation?
The second condition of a for loop does not need to contain a value. It is a condition and can be a function itself. Usually the function should not be placed such that it is not reiterated with each loop, but when it is conditional uppon the iteration of the loop that is quite valid.
Edit: tasairis and I are on the same page it seems. He types faster than I do.
Edit: tasairis and I are on the same page it seems. He types faster than I do.
Code: Select all
for($key=10; array_key_exists($key, $array); $key+=1){
etc...
Re: better to use for or foreach in this situation?
I do know that the keys will be numeric. So, if I understand correctly, using a for loop with the second condition being array_key_exists($key, $array), I wouldn't even need the else{break;}, because the for loop will end as soon as it reaches an array key that doesn't exist, right? That seems to be the simplest solution in this particular case. Many thanks for the tips, Bill and tasairis. I'm just about a month into teaching myself PHP, and this forum is turning out to be quite a helpful resource when I need some explanation beyond what I've found in my books and in various online tutorials.
Cheers,
Drew
Cheers,
Drew
Re: better to use for or foreach in this situation?
If the input is coming from a form (which you said) then you don't know that. 99% of the time it will be, but the problem is those annoying little buggers who like to screw around with people's sites.oboedrew wrote:I do know that the keys will be numeric.
Just to be sure, put the array through array_values first. Can't hurt to be extra careful.
Re: better to use for or foreach in this situation?
I should be more specific regarding the way in which the number of elements in the array is determined by user input. This project is a blog using a flat file database. Each entry is assigned a unique id number. When the blogger publishes a new entry, the id number is generated and appended to the end of a text file containing a simple list of those id numbers (one per line). Another script reads this text file as an array using the file function. So the quantity of elements in the array is determined by user input (over time, not by just a single form submission), but the user enters neither the keys nor the values into a form. I am not aware of any way someone could cause a key to be anything other than a number. Am I mistaken? Perhaps underestimating the capabilities of the mischievous and php-savvy?
By the way, can anyone recommend an especially good book or online tutorial on security issues and php? The books from which I am learning cover many basic security measures, but I suspect they are only scratching the surface, given the apparent complexity of the topics.
Cheers,
Drew
By the way, can anyone recommend an especially good book or online tutorial on security issues and php? The books from which I am learning cover many basic security measures, but I suspect they are only scratching the surface, given the apparent complexity of the topics.
Cheers,
Drew
Re: better to use for or foreach in this situation?
Ha. I knew it.
I was going to include a line in my post about assuming that this input came from an HTML form as opposed to another source...
If you're using file() then you're fine.
As for security tutorials... Don't know. The PHP-Security forum probably has a better idea. In a nutshell, don't trust anything that doesn't come from your own code.
I was going to include a line in my post about assuming that this input came from an HTML form as opposed to another source...
If you're using file() then you're fine.
As for security tutorials... Don't know. The PHP-Security forum probably has a better idea. In a nutshell, don't trust anything that doesn't come from your own code.