Re: Re: Trying to understand what is happening in this code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/12/2013 1:43 AM, Robert Cummings wrote:
On 13-10-11 01:52 PM, Jim Giner wrote:
On 10/11/2013 11:01 AM, Nathan Grey wrote:
Hi - I am new to PHP so this is probably a very rudimentary question. I
posed it over at Stack Overflow and got several responses but none of
them
clarified the issue for me.

http://stackoverflow.com/questions/19309369/trying-to-understand-how-these-php-sections-work-together


Here's the code I am trying to understand:

<body>
      <h1>The first twenty Fibonacci numbers:</h1>
      <ul>
      <?php
      $first = 0;
      $second = 1;
      for ($i = 0; $i < 20; $i++) {
          ?>
          <li><?php echo $first + $second ?></li>
      <?php
          $temp = $first + $second;
          $first = $second;
          $second = $temp;

      } ?>
      </ul></body>

This code produces an unordered list of the first 20 Fibonacci
numbers. I
understand that HTML and PHP can be interspersed. I also understand that
the PHP processor will look over the code and pick up the code that is
between the opening and closing PHP tags. Here's where I am confused:

1. Can the processor really reassemble a for-loop that is broken into
segments by opening and closing tags? It seems like the integrity of the
loop would be broken. I would expect that the entire loop would have
to be
contained within a single pair of opening and closing tags for it to
work.

2. Even if the processor is able to reassemble the loop seamlessly,
how do
the <li> tags get included in the loop since they fall outside of the
PHP
tags?

Thanks for your help.

Nathan

-----------------------------
Nathan Grey

Perhaps this whole long discussion could have been prevented if you
DIDN'T intersperse the php and html.  Code (IMHO) is much easier to read
and write when you separate the logic from the presentation.

How about this?
<?
$ulist = "<h1>The first twenty Fibonacci numbers:</h1>";
$ulist .= "<ul>";
$first = 0;
$second = 1;
for ($i = 0; $i < 20; $i++)
{
       $temp = $first + $second;
       $ulist.= "<li>$temp</li>";
       $first = $second;
       $second = $temp;
}
$ulist .= "</ul>";
.
.
.
finish processing
.
.
DisplayPage($ulist);
exit();
//********************
//********************
//********************
function DisplayPage($ulist)
{
     $code =<<<heredocs
     (std. html startup lines)
      <body>
      .
      .
      $ulist
      .
      (rest of html, etc.)
      .
      .
      </body>
      </html>
heredocs;
      echo $code;
      return
}
?>

All done with one pair of php tags. and logic and presentation separated.

But in practice, if you aren't switching in and out of PHP then it's
best to not bother closing the PHP tag at the end lest you run into
session issues due to an invisible trailing space on an included library.

Cheers,
Rob.
I only did that in this example to benefit the OP and to mimic his style. I don't ever close php, but I will have to read up on your point. :)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux