Question

  • Creator
    Topic
  • #2211058

    What is the output for this PHP for loop?

    Locked

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

All Answers

  • Author
    Replies
    • #2901620

      Clarifications

      by sanchezr1 ·

      In reply to What is the output for this PHP for loop?

      Clarifications

      • #2901609

        Request for Clarification

        by avinashreddy1090 ·

        In reply to Clarifications

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

      • #2901590

        Request for Clarification

        by tony hopkinson ·

        In reply to Clarifications

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

      • #2901582

        Request for Clarification

        by sanchezr1 ·

        In reply to Clarifications

        Well I am trying to understand the output for the code

      • #2901581

        Request for Clarification

        by sanchezr1 ·

        In reply to Clarifications

        Since I dont seem to understand the output very well

    • #2901574

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

      by tony hopkinson ·

      In reply to What is the output for this PHP for loop?

      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.

    • #2901757

      Try it yourself??

      by navdeepz ·

      In reply to What is the output for this PHP for loop?

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

    • #2901746

      Slight explanation

      by noshmon ·

      In reply to What is the output for this PHP for loop?

      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.

    • #2901726

      Control structure nesting

      by jkiernan ·

      In reply to What is the output for this PHP for loop?

      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.

    • #2901717

      Some real help

      by spitfire_sysop ·

      In reply to What is the output for this PHP for loop?

      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.

Viewing 5 reply threads