/    Sign up×
Community /Pin to ProfileBookmark

How to practice on nested for loop in php

Hi. Is it possible that to have nested for loop in php? I searched it out, there are so many examples but i don’t understand. I sent many time on it but couldn’t grasp the idea. Please guide me that how is it possible to use it properly. I’m new to php and web-development. Thanks.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 06.2015 — [code=php]
<?php
echo "<h1>Multiplication Table</h1>n";
for($i = 1; $i <= 10; $i++)
{
for($j = 1; $j <= 10; $j++)
{
echo "<p>$i * $j = ".($i * $j)."</p>n";
}
}
[/code]

WARNING: untested
Copy linkTweet thisAlerts:
@AdqusitauthorJan 07.2015 — Hello ND. Thanks for you reply.

1 * 1 = 1

1 * 2 = 2

1 * 3 = 3

1 * 4 = 4

1 * 5 = 5

1 * 6 = 6

1 * 7 = 7

1 * 8 = 8

1 * 9 = 9

1 * 10 = 10[/QUOTE]

This is the result of the code, but its just a small piece I copied from browser.

Would you please make a dry run for me of this program. Actually the thing which i started this thread is that I don't know that how this program goes line by line. how each line is executed in this nested loop. Like in C++ I used to press the F8 key to run the code line by line and by this way one could easily understand that which loop runs how many times.
Copy linkTweet thisAlerts:
@ginerjmJan 07.2015 — PHP may have a debugger that you can use to do that, but it doesn't do it natively - that I know of.

Try adding echo statements in the loops if you are that confused about what is going on.

What do you mean you don't know how this program goes 'line by line'? Do you not use PHP? Perhaps it is time to do a little learning?
Copy linkTweet thisAlerts:
@AdqusitauthorJan 08.2015 — Hello ginerjm.

Thing which i want to know is that as i said earlier that in C++ we can check the flow of program line by line by pressing F8 key in compiler.

The dry run i asked because I have a program which makes the pattern i.e.
<i>
</i>for($ab = 1; $ab &lt;= 8; $ab++)
{
for($bc = 1; $bc &lt;=$ab; $bc++)
{
echo "*";
}
echo "&lt;br&gt;";
}

This code makes the * pattern, increasing one by one downwards.

I studied the nested for loop both in C++ and php but what it says, it does not show the output same way. I followed the homeandlearn website for detail but couldn't get the idea properly. Thanks if someone can guide me. ?
Copy linkTweet thisAlerts:
@ginerjmJan 08.2015 — What pattern are you seeking?
Copy linkTweet thisAlerts:
@AdqusitauthorJan 08.2015 — payramid shape.
Copy linkTweet thisAlerts:
@AdqusitauthorJan 08.2015 — Inside the first loop, the nested loop has a condition $bc <= $ac? If i remove the equal to sign, so it should give me error but it does not and produce the same result?
Copy linkTweet thisAlerts:
@ginerjmJan 08.2015 — I kind of thought you would demonstrate visually....
Copy linkTweet thisAlerts:
@NogDogJan 08.2015 — Inside the first loop, the nested loop has a condition $bc <= $ac? If i remove the equal to sign, so it should give me error but it does not and produce the same result?[/QUOTE]

No, it's not an error. It just changes the comparison from "less than or equal to" to "less than", which would result in one less row of "*" characters being output.
×

Success!

Help @Adqusit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...