Questions

What is the output for this PHP for loop?

Tags:
+
1 Votes
Locked

What is the output for this PHP for loop?

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

Clarifications

avinashreddy1090

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

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

sanchezr1

Well I am trying to understand the output for the code

sanchezr1

Since I dont seem to understand the output very well

  • +
    3 Votes
    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.

    +
    0 Votes
    navdeepz

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

    +
    0 Votes
    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.

    +
    0 Votes
    sanchezr1

    Thank you noshmon

    +
    1 Votes
    jkiernan

    http://writecodeonline.com/php/

    http://www.php.net/manual/en/control-structures.for.php

    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.

    +
    0 Votes
    Spitfire_Sysop

    I believe this is what you are looking for:
    http://writecodeonline.com/php/

    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.

  • +
    3 Votes
    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.

    +
    0 Votes
    navdeepz

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

    +
    0 Votes
    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.

    +
    0 Votes
    sanchezr1

    Thank you noshmon

    +
    1 Votes
    jkiernan

    http://writecodeonline.com/php/

    http://www.php.net/manual/en/control-structures.for.php

    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.

    +
    0 Votes
    Spitfire_Sysop

    I believe this is what you are looking for:
    http://writecodeonline.com/php/

    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.