#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    21
    Rep Power
    0

    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
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    32
    Please post your current code so we can help
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    21
    Rep Power
    0
    for($i=0; $i<1; $i++){
    echo "*";
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    21
    Rep Power
    0
    it will show one star but I can not figure out how to get it to do the rest of the lines and with only using 3 lines not counting opening and closing php thought I better mention that
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    32
    Sorry what do you mean only 3 lines? Is this some form of homework and the specification is you can only use 3 lines of code to get the desired result?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    21
    Rep Power
    0
    yes it is home work I am not wanting the exact answer just trying to figure out how to do it like trying to get on the right track just confused we are supposed to make it with one for loop and it can be done in 3 lines is what it says but my code is not working no matter how I change the numbers just trying to figure out what I am doing wrong.
    We talked about nested for loops. We saw an example of one:

    for($lineNumber=1;$lineNumber<=5;$lineNumber++) {
    for($starCount=1;$starCount<=$lineNumber;$starCount++) {
    echo("*");
    }
    echo("<br />");

    }

    OUTPUT:

    *

    **

    ***

    ****

    *****

    This can be completed without using a nested for loop, instead just using one for loop. No other loops are necessary in this structure either. (No whiles or anything other than the initial for loop). ALSO - NO IFS, OR SWITCH STATEMENTS! No functions! No arrays are necessary either. Inside of one for loop, you can complete this in a maximum of three lines of code (not counting the opening and closing PHP tags).
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    32
    I am really sorry but I can't help as I just can't think of a way to do this myself with only one for loop and 3 lines of code.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Originally Posted by simplypixie
    I am really sorry but I can't help as I just can't think of a way to do this myself with only one for loop and 3 lines of code.
    Hint: http://php.net/manual/en/function.str-repeat.php

    Edit: sorry, functions not allowed..
    Last edited by Aurum84; February 9th, 2013 at 06:16 AM.
  16. #9
  17. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,539
    Rep Power
    2337
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  18. #10
  19. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,298
    Rep Power
    7170
    There are two operators you need to know about to do this:
    the string concatenation operator
    comma operator (within the context of a for loop)

    You can actually do it in just one line (although the definition of a "line" is somewhat fuzzy).
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Curious. With the 3 lines specified... Is this counting the opening/closing of your for() statement? Or 3 lines within it?

    If 3 lines total, don't do anything using a numeric variable. Instead of your $i++, use $star .= "*"
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    21
    Rep Power
    0
    This seems to be a very confusing topic but I appreciate everyone's help. It has to be 3 lines total not counting opening and closing php as far as for goes I have that on my first line of code I have a lot of different ways I try but they all leave me with one line of *'s not actual looping as I will need 5 rows and they need to start one star on the first 2 on the second 3 on the third four on the fourth and five on the fifth lol it is not as simple as I thought it would be and it is not even a big assignment in our course either I am usually very good at this stuff but this one is just driving me up a tree it is for extra credit and not much any way so I might just give up because I worked on it for about a week and then contacted the forum and I still can not figure it out I just hate being beat by this stuff, but in real life in my career I guess I would just use a else or two for loops and it would work out fine. lol Thanks for all your help and sorry this thing seems impossible.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Sorry. I figure I'd maybe toss this on in just for future ideas..

    PHP Code:
    <?php
      
    for($star "*";$star != "******";$star .= "*") {
        echo 
    $star "<br />";
      }
    ?>
    1st expression defines a star.
    2nd expression makes sure we haven't hit 6 stars yet.
    3rd expression adds a star after each run.

    Comments on this post

    • Jacques1 agrees : Nice. :-D
    • simplypixie agrees : So simple :)
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    21
    Rep Power
    0
    Originally Posted by Triple_Nothing
    Sorry. I figure I'd maybe toss this on in just for future ideas..

    PHP Code:
    <?php
      
    for($star "*";$star != "******";$star .= "*") {
        echo 
    $star "<br />";
      }
    ?>
    1st expression defines a star.
    2nd expression makes sure we haven't hit 6 stars yet.
    3rd expression adds a star after each run.
    seems to work great thanks

IMN logo majestic logo threadwatch logo seochat tools logo