• Welcome to ForumKorner!
    Join today and become a part of the community.

Print list of numbers

OliverE

Power member.
Reputation
0
Ive got this code thats looking at C to see if its even, if it is it adds it to D and then C is increased by 2 to get the next even number.
I know ive done it a wierd way, but it hay.

Anyway, my issue is, is that I want the numbers to display like this:
30 32 34 36 38 40
but at the moment its just summing all the numbers together.

Any ideas?
Thanks

Pritty simple stuff, but I dont work with numbers in PHP.

PHP:
$c = 30;
while($c>=30 && $c<=40 && $c%2==false):
$d = $d + $c;
$c=$c+2;
endwhile;

So you know, i then use the D variable somewhere else in my code.
 

Factor8™

Active Member
Reputation
0
Should be like this:

PHP:
$c = 30;
while($c>=30 && $c<=40 && $c%2==false) {
$d = $d + $c;
$c=$c+2;
}

Also, what are you trying to say with this statement;

$c%2==false

What you're saying there is, the remainder of 30 and 2 is false.
 

OliverE

Power member.
Reputation
0
Not what I'm after my code works but it sums d and c together and need to have a list.of the numbers generated by the loop.

But I think I've found out what I need to do, will test it later.
 

Factor8™

Active Member
Reputation
0
Instead of $d = $d + $c

Concatenation is your answer. Like this.

$d = $d . $c

Yes. The period is supposed to be there.
 

OliverE

Power member.
Reputation
0
Yep, thats what I was gonna try.
Should work.

I tried everything like converting to strings then adding, but then I remembered while in my Lecture this afternoon.

Hows my PHP challenge going btw?
 

Factor8™

Active Member
Reputation
0
I'm not good enough to do your challenge.

On second thought, I've learned a lot since then.

I'm doing C right now, so no time.
 

OliverE

Power member.
Reputation
0
Yer thats sorted it.
PHP:
$d = $d . " " . $c;
Next task!
 

Factor8™

Active Member
Reputation
0
Nice job. Glad to helPr

So this became the final code?
PHP:
$c = 30;
while($c>=30 && $c<=40 && $c%2==false) {
$d = $d . " " . $c;
$c=$c+2;
}

Again, what the heck is this for:

PHP:
$c%2==false
 
Top