943,605 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 543
  • PHP RSS
Jul 20th, 2009
0

Please advice how the follwoing code works

Expand Post »
I dun quite understand how this program works.......
Please advice...
What is the output of this program?

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <!-- test01.php -->
  3. <body>
  4.  
  5. <?php
  6. $x = 2;
  7. for ($i = 1; $i <= 6; $i++)
  8. {
  9. if ($i % 2 == 1)
  10. {
  11. $x++;
  12. }
  13. print $i . 'x' . $i * $x . " ";
  14. }
  15.  
  16. ?>
  17.  
  18. </body>
  19. </html>
  20.  
  21.  
  22.  
  23.  
  24.  
  25. *******
  26. Answer:
  27.  
  28. 1x3 2x6 3x12 4x16 5x25 6x30

Please advice...
Thanks,

Tony
Reputation Points: 10
Solved Threads: 0
Light Poster
Jintu is offline Offline
38 posts
since Jun 2009
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

This sounds like a homework problem to me, maybe you should to a little more studying. Why else would you include the "answer."
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

Click to Expand / Collapse  Quote originally posted by paradox814 ...
This sounds like a homework problem to me, maybe you should to a little more studying. Why else would you include the "answer."
This is an example of the kind of question that might appear in the final exam and although I have done 4 weeks of PHP, I dun quite get the reason for the output, which was given by my prof...
Reputation Points: 10
Solved Threads: 0
Light Poster
Jintu is offline Offline
38 posts
since Jun 2009
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

You've spent more time on daniweb than hitting your books. Not a bad thing to do, but you need to do your own work now.

Clue: what type of numbers does $i % 2 == 1 give you??
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,664 posts
since Oct 2006
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

Click to Expand / Collapse  Quote originally posted by ardav ...
You've spent more time on daniweb than hitting your books. Not a bad thing to do, but you need to do your own work now.

Clue: what type of numbers does $i % 2 == 1 give you??
I guess, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, etc...
Am I correct?
Reputation Points: 10
Solved Threads: 0
Light Poster
Jintu is offline Offline
38 posts
since Jun 2009
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

Give the man a cigar! Now work out the relationship to the output or series. Oops almost gave it away.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,664 posts
since Oct 2006
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

Click to Expand / Collapse  Quote originally posted by ardav ...
Give the man a cigar! Now work out the relationship to the output or series. Oops almost gave it away.
X = 2 rite,
Then howcome the answer to the code is:
1 x 3, 2 x6, 3 x 12, 4 x16, 5 x 25, 6 x30

x++(shouldn't it be 3, 5, 7, 9, 11, 13)

I am confused...
Reputation Points: 10
Solved Threads: 0
Light Poster
Jintu is offline Offline
38 posts
since Jun 2009
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

Click to Expand / Collapse  Quote originally posted by Jintu ...
X = 2 rite,
Then howcome the answer to the code is:
1 x 3, 2 x6, 3 x 12, 4 x16, 5 x 25, 6 x30

x++(shouldn't it be 3, 5, 7, 9, 11, 13)

I am confused...
Mais non, mon cheri.
Quote originally posted by Blaise Pascal ...
You always admire what you really don't understand
It would be wrong of me to arrest your admiration.
Last edited by ardav; Jul 20th, 2009 at 1:41 pm.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,664 posts
since Oct 2006
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

Click to Expand / Collapse  Quote originally posted by ardav ...
Mais non, mon cheri.


It would be wrong of me to arrest your admiration.
Could you please help me one last time on this, I am really lost...

Thanks,

Tony
Reputation Points: 10
Solved Threads: 0
Light Poster
Jintu is offline Offline
38 posts
since Jun 2009
Jul 20th, 2009
0

Re: Please advice how the follwoing code works

Paid รข bod yn wirion, gwranda, rwyt wedi derbyn sawl darn o god yn rhad ac am ddim, cer i wneud gwaith dy hun.

In a word, no.

I think your semester average has been bumped enough already. You get the grades you deserve at the end.

False dawns and dead ends are the making of men. What feeble excuse of a man shall you turn out to be if you continue to complete a crossword by first checking the answers?

Go forth and pound thy books.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,664 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need Help in Implementing eway payment gateway.
Next Thread in PHP Forum Timeline: I'm way out of my depth and need desperate help.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC