6 Replies - 191 Views - Last Post: 09 February 2013 - 02:58 AM
#1
need help with simple for loop new to PHP and forum
Posted 09 February 2013 - 01:13 AM
*
**
***
****
*****
Replies To: need help with simple for loop new to PHP and forum
#2
Re: need help with simple for loop new to PHP and forum
Posted 09 February 2013 - 01:50 AM
First, let's take a look at what a for loop does. A for loop is usually given three expressions, separated with a ';' symbol. The first expression is executed (basically, ran as if it were on it's own line). The second expression acts as an if statement. If it's true, the loop will continue. If it's false, the loop will not continue and the rest of the script will continue. The third expression is executed just like the first.
A simple example of this would be:
for($i=0; $i<5; $i++){
echo "Hello!";
}
This will output "Hello!" five times.
We are also able to access the variables we modified in our for statement. An example of this would be:
for($i=10; $i<15; $i++){
echo "I am " . $i . " years old.";
}
This will produce:
Quote
I am 11 years old.
I am 12 years old.
I am 13 years old.
I am 14 years old.
Now you know how to use a for loop! You can do what you want by multiplying the number of stars you need to display by the current variable you are using (like in our examples, $i).
Hope this helps!
This post has been edited by creativecoding: 09 February 2013 - 01:52 AM
#3
Re: need help with simple for loop new to PHP and forum
Posted 09 February 2013 - 02:17 AM
#4
Re: need help with simple for loop new to PHP and forum
Posted 09 February 2013 - 02:23 AM
Don't forget, we're always open to helping you learn the material and such. It's the whole doing someone else's homework that's against the rules.
Here's the link to the "for loop" documentation: http://www.php.net/m...uctures.for.php
Good luck on your studies!
This post has been edited by creativecoding: 09 February 2013 - 02:23 AM
#5
Re: need help with simple for loop new to PHP and forum
Posted 09 February 2013 - 02:31 AM
<?php
for($i=0; $i<1; $i++){
echo "*";
}
?>
which only displays one star on the first line which is good but how do I specify other lines too I did figure out if I use a <br> I can make more lines to make it look correct but the code will not be right because I am only suppose to use the 3 lines if there is anyway you can help me with out totally giving me the answer I would really appreciate it thanks.
#6
Re: need help with simple for loop new to PHP and forum
Posted 09 February 2013 - 02:39 AM
Right now you're saying "$i is 0. If $i is less than 1, increase it by one and say '*'".
You want it to say "$i is 0. If $i is less than 5" - because remember we're starting at zero - ", increase it by one and say '*' times whatever $i is"
Your second expression is where this happens ($i<1 in your code). This is saying "$i is less than 1", which will only be true once when $i is 0 (as it started at). After that returns true the next expression will be ran and $i would equal 1 and would no longer be less than one, so the for loop would be over.
This post has been edited by creativecoding: 09 February 2013 - 02:41 AM
#7
Re: need help with simple for loop new to PHP and forum
Posted 09 February 2013 - 02:58 AM
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).
This post has been edited by JackOfAllTrades: 09 February 2013 - 05:18 AM
Reason for edit:: Added code tags
|
|

New Topic/Question
Reply



MultiQuote






|