PHP confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekphukan
    New Member
    • Jul 2014
    • 34

    PHP confusion

    (Can anyone explain me the if satetment in this example neatly...i m not getting at all..
    Thank You!!!)


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
        <title>Fibonacci sequence</title>
        <link rel="stylesheet" type="text/css" href="common.css" />
        <style type="text/css">
          th { text-align: left; background-color: #999; }
          th, td { padding: 0.4em; }
          tr.alt td { background: #ddd; }
        </style>
      </head>
      <body>
    
        <h2>Fibonacci sequence</h2>
    
        <table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
          <tr>
            <th>Sequence #</th>
            <th>Value</th>
          </tr>
          <tr>
            <td>F<sub>0</sub></td>
            <td>0</td>
          </tr>
          <tr class="alt">
            <td>F<sub>1</sub></td>
            <td>1</td>
          </tr>
    <?php
    
    $iterations = 10;
    
    $num1 = 0;
    $num2 = 1;
    
    for ( $i=2; $i <= $iterations; $i++ )
    {
      $sum = $num1 + $num2;
      $num1 = $num2;
      $num2 = $sum;
    ?>
          <tr<?php if ( $i % 2 != 0 ) echo ' class="alt"' ?>>
            <td>F<sub><?php echo $i?></sub></td>
            <td><?php echo $num2?></td>
          </tr>
    <?php
    }
    ?>
        </table>
      </body>
    </html>
    Last edited by Rabbit; Mar 13 '15, 02:09 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The percent operator divides by the number and returns the remainder.

    Comment

    • abhishekphukan
      New Member
      • Jul 2014
      • 34

      #3
      i know that...but what is the function of the echo statement there..."echo class=alt"

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        It prints a class statement into the HTML on every other line. Giving every other line a different CSS class.

        Comment

        • abhishekphukan
          New Member
          • Jul 2014
          • 34

          #5
          I m still not getting...it may sound silly but i m new to php..:(
          Well thanks for your time and answer.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            This is usually done to make every other row look different. You're new to php but you know HTML and CSS right? Just run the php code and you can see what the resulting HTML looks like.

            Comment

            • abhishekphukan
              New Member
              • Jul 2014
              • 34

              #7
              Alright sir...definatel y i will go for it.

              Comment

              Working...