What is the output for this PHP for loop?

By sanchezr1 ·
Hi guys , could somebody be kind enough to explain the output for this code. thank you very much

for($i=0; $i < 3 ; $i=$i + 1) {
for ($j=0; $j < 3; $j =$j + 1) {
for ($k=0;$k < 3; $k =$k + 1){
print " I: $i; J: $j; K: $k\n" ;

This conversation is currently closed to new comments.

11 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +


Collapse -
by avinashreddy1090

does this code belong to PHP, because to print something 'echo' is used not 'print'

Collapse -
by Tony Hopkinson

Actually print and echo are near enough the same thing and both or either can be used.

My question would be why does anyone who needs to know the answer need to ask the question.....

Collapse -
by sanchezr1

Well I am trying to understand the output for the code

Collapse -
by sanchezr1

Since I dont seem to understand the output very well

All Answers

Collapse -

Well I'm left with the horrible feeling that this is homework

by Tony Hopkinson In reply to What is the output for th ...

of some sort and if it is, you are unlikely to get an answer, because I don't think anyone who couldn't figure it out, should be allowed anywhere near any code and I know anyone who can't be bothered to, shouldn't.

Just give up programming altogether you are totally unsuited to it.

Collapse -

Try it yourself??

by navdeepz In reply to What is the output for th ...

why don't you just like try doing it yourself... and you know get the output .. ???

Collapse -

Slight explanation

by Noshmon In reply to What is the output for th ...

Hi Sanchez. Although the various other posts are right, and you really should work this out yourself I'm going to offer a little help by explaining a bit. Please note that everything I'm going to say I have learnt myself from W3Schools. You really should check it out if you want to understand anything properly.

The for creates a loop until a specified goal is reached. In your example it's all the time the variable is less than 3.

The next bit declares the variable ($x=0). Hopefully you can work out what that means.

The next bit is the statement to check against before continuing the loop. As I said, in your example it checks to see if the variable is less than 3.

$x=$x+1 reads the variable X and adds one to it.

I'm going to leave you to work out what the output would be. Although just running it would be an option I suggest you try and work out the output before you run the code to ensure you have an understanding. If you get the number wrong then you know you'll have to go back and better your understanding.

If you're genuinely interested and want to learn then I'll be happy to help, and create a few exercises for you to work on. If, however, you have no interest in learning and just want us to do your work for you then please don't waste our time.

Collapse -

Reponse To Answer

by sanchezr1 In reply to Slight explanation

Thank you noshmon

Collapse -

Control structure nesting

by jkiernan In reply to What is the output for th ...

You need to understand the nesting of the FOR structures. The code in the innermost braces runs first. This might be easier to follow if you indent the code.

Collapse -

Some real help

by Spitfire_Sysop In reply to What is the output for th ...

I believe this is what you are looking for:

Just drop your code in here to test it and see the results live. Now you don't need your own webserver just to try your code.

Back to Peripheral Forum
11 total posts (Page 1 of 2)   01 | 02   Next

Related Discussions

Related Forums