Jump to content

Nested loop exercise


noob321

Recommended Posts

HI all i need to write a small nested loop example to output a series of integers in the following format:

1

12

123

1234

12345

 

where the 1 is 10px, 2 is 20px font size etc. 

i have got to to output 1,2,3,4,5 but not my above desired output , can anyone point me in the right direction?

heres my code:
 

<?php
$sizes =array("10px","20px","30px","40px","50px");
 
for($i = 0, $l = count($sizes); $i < $l; $i++){
  $size = $sizes[$i];
  $text = $i + 1;
  echo "<span style='font-size:$size'>$text</span><br/>";
}
?>
Link to comment
https://forums.phpfreaks.com/topic/287557-nested-loop-exercise/
Share on other sites

You need to concatenated $i + 1 to $text for each iteration of the loop.

$text .= $i + 1; // concatenate $i + 1

Before the for loop make sure you initialise $text before hand too (  $text = '';  ) to prevent a  notice message.

 

The .= is the concatenation assignment operator

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.