Jump to content

Nested loop exercise


Go to solution Solved by Ch0cu3r,

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

  • Solution

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

Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.