954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please advice how the follwoing code works

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

<html>
<!-- test01.php -->
<body>

   <?php 
      $x = 2;
      for ($i = 1; $i <= 6; $i++)
      {
          if ($i % 2 == 1)
          {
             $x++;
          }
          print $i . 'x' . $i * $x . " ";
      }

   ?>

</body>
</html>





*******
Answer: 

1x3  2x6  3x12  4x16  5x25  6x30


Please advice...
Thanks,

Tony

Jintu
Light Poster
38 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This sounds like a homework problem to me, maybe you should to a little more studying. Why else would you include the "answer."

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 
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...

Jintu
Light Poster
38 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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?

Jintu
Light Poster
38 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Give the man a cigar! Now work out the relationship to the output or series. Oops almost gave it away.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 
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...

Jintu
Light Poster
38 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.You always admire what you really don't understand

It would be wrong of me to arrest your admiration.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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

Jintu
Light Poster
38 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You