Notice: You have been identified as a bot, so no internal UID will be assigned to you. If you are a real person messing with your useragent, you should change it back to something normal.

Minichan

Topic: Someone explain foreach loops.

Anonymous A started this discussion 15 years ago #6,990

I just don't understand them. At all. I thought they would be similar to for loops, but apparently not. And if they are I don't see how.

Halp. :(

(Edited 2 minutes later.)

r04r joined in and replied with this 15 years ago, 1 hour later[^] [v] #122,431

Which language? The syntax differs a lot.

They simply iterate over all items in a collection.

protip !!pMmAiEwOm joined in and replied with this 15 years ago, 34 minutes later, 1 hour after the original post[^] [v] #122,475

Generally, they’re (in pseudocode)…

for each (collection of stuff) as (symbol that will contain one item) {
do something
}

Anonymous D joined in and replied with this 15 years ago, 5 minutes later, 1 hour after the original post[^] [v] #122,481

Image went missingFor loops and foreach loops don't actually have anything to do with each other

$ages = array(1, 2, 9, 102, 12, 3);

Based on this shitty array, the foreach loop is going to execute 6 times - using each element of the array.

Now the actual motherfucking loop:
foreach($ages as $age) {
echo $age . ", ";
}

That's going to take the array $ages we created, and all of the elements are going to be asigned the value $age. We haven't defined $age anywhere yet; $age is a temporary variable that the loop is going to use for each element in the array.

At the start, $age is going to be equal to 1, and it will output 1 with a comma on it, and so on all the way through the array.

It's useful when you want to traverse an array, if you want to do something to every element in an array, like if you had a big list of things and you wanted to сheck whether a thing has сertain properties, etc. Or maybe there's some extra whitespaсe you want to remove.

Is that what you wanted to know? :')

(Edited 2 minutes later.)

r04r replied with this 15 years ago, 4 minutes later, 1 hour after the original post[^] [v] #122,485

@previous (D)
> 1 with a comma on it

What. It'll just output 1.

Anonymous D replied with this 15 years ago, 1 minute later, 1 hour after the original post[^] [v] #122,486

@previous (r04r)
No it wont!

protip !!pMmAiEwOm replied with this 15 years ago, 1 minute later, 1 hour after the original post[^] [v] #122,487

Paging The Truth and Dachan-da



r04r just wrote something about code. Please appear and call her a stupid fat whore who can’t code lololololol

Anonymous D replied with this 15 years ago, 55 seconds later, 1 hour after the original post[^] [v] #122,488

@previous (protip !!pMmAiEwOm)
Lol The Truth.

(Edited 27 seconds later.)

r04r replied with this 15 years ago, 1 minute later, 1 hour after the original post[^] [v] #122,490

@122,486 (D)
Oh, I thought you meant the variable itself. I should lrn2read.

Derp.

Anonymous A (OP) replied with this 15 years ago, 5 minutes later, 1 hour after the original post[^] [v] #122,494

oh shit. Thanks guise!

Anonymous A (OP) double-posted this 15 years ago, 33 seconds later, 1 hour after the original post[^] [v] #122,495

@122,481 (D)
> David Cameron
> :')
Sup killer lettuce?

Anonymous E joined in and replied with this 15 years ago, 6 minutes later, 2 hours after the original post[^] [v] #122,510

Image went missing@previous (A)
> implying that's david cameron

Anonymous A (OP) replied with this 15 years ago, 44 seconds later, 2 hours after the original post[^] [v] #122,512

$ages = 1, 2, 9, 102, 12, 3;

foreach ($ages is $age) {
echo $age . ", ";


Parse error: syntax error, unexpected ',' in C:\wamp\www\PHP\php.php on line 4

why doesn't it let me use commas?

TTEH replied with this 15 years ago, 46 seconds later, 2 hours after the original post[^] [v] #122,513

Image went missing@previous (A)
All you had to do was copy and paste. :(

$ages = array(1, 2, 9, 102, 12, 3);
foreach($ages as $age) {
echo $age . ", ";
}


Put the array's values in parentheses btw.

(Edited 2 minutes later.)

TTEH double-posted this 15 years ago, 1 minute later, 2 hours after the original post[^] [v] #122,514

@122,495 (A)
Gordon Brown. :')

Anonymous A (OP) replied with this 15 years ago, 2 minutes later, 2 hours after the original post[^] [v] #122,518

@122,513 (TTEH)

i like typing it myself, it's easier to familiarize myself with it that way.

Anonymous A (OP) double-posted this 15 years ago, 3 days later, 3 days after the original post[^] [v] #126,893

Okay, last question. This time it's about the return function.

Say I had

function something($var1, var2) {
$sum = $var1 + $var2;
return $sum;


What would the
return $sum
do?

Killer Lettuce !!iNo3FkiZx joined in and replied with this 15 years ago, 56 seconds later, 3 days after the original post[^] [v] #126,894

@122,495 (A)
Protip: I don't code.

Anonymous G joined in and replied with this 15 years ago, 18 minutes later, 3 days after the original post[^] [v] #126,904

@126,893 (A)
I returns $sum as the result of the function. It specifies what gets assigned to $derp when
derp = something(1,2)
is called elsewhere in your code. (assuming you have something() properly set up to return values in whatever language you are using)

Anonymous A (OP) replied with this 15 years ago, 9 minutes later, 3 days after the original post[^] [v] #126,914

@previous (G)
Oh I get it now. Thank you! I know it's a DM-style noob question, but it has been bugging me for hours.
:

Please familiarise yourself with the rules and markup syntax before posting.