SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with simple for loop new to PHP and forum

    I need to figure out how to make 5 rows of * and only use one for loop it has been driving me nuts all week any help is greatly appreciated because I have tried so many different ways. I am trying to create this but with only one for loop in php
    *
    **
    ***
    ****
    *****

  2. #2
    SitePoint Member
    Join Date
    Feb 2013
    Location
    Croatia
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello. This is one simple but yet effective codeish, hope you find it helpful.

    PHP Code:
    <?php

    $addingvar 
    '*'// our pattern
    $var ''// just to call variable 

    foreach(range(1,5) as $value) {
        
    $var .= $addingvar;
        echo 
    $var '<br />';
    }
    ?>
    What I did there was added two variables (one is our pattern, in this case * while another one is variable that will recieve pattern each time loop repeats).
    foreach function is something you should already know of, if not check its manual . I added simple range function instead of doing array(1,2,3,4,5). If you have any other questions let me know.

    Best regards, Dino


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •