Question Locked

What is the output for this PHP for loop?

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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" ;
}
}
}

11 replies

by avinashreddy1090

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

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.....

by sanchezr1

Well I am trying to understand the output for the code

by sanchezr1

Since I dont seem to understand the output very well

by tony hopkinson

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.

by navdeepz

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

by noshmon

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.

This discussion is closed to new replies.