Re: Trying to understand what is happening in this code

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

 



Everyone kept saying this to me, and now I think I understand why, but
initially, it just added to the confusion because I couldn't figure out how
or why a php command ("echo") was suddenly being applied to content outside
of the PHP tags. Deus ex machina. It just seemed to deepen the mystery. In
fact, what people were saying (I think) is "if you are confused because
sections of PHP and HTML are appearing together in a single script, then
just try thinking about it as if it were one long PHP script."


-----------------------------
Nathan Grey
404-337-9005


On Mon, Oct 14, 2013 at 5:25 AM, Ford, Mike <M.Ford@xxxxxxxxxxxxxx> wrote:

> Just to add another perspective to this, the response I've often seen to
> this sort of enquiry is that you should regard
>
>    ?>whatever<?php
>
> as the equivalent of
>
>    ; echo "whatever";
>
> (with appropriate escaping applied to "whatever", of course).
>
>
> Cheers!
>
> Mike
>
> --
> Mike Ford,
> Electronic Information Developer, Libraries and Learning Innovation,
> 403a Leslie Silver Building, City Campus, Leeds Metropolitan University,
> Woodhouse Lane, LEEDS,  LS1 3ES,  United Kingdom
> E: m.ford@xxxxxxxxxxxxxx     T: +44 113 812 4730
>
>
> > -----Original Message-----
> > From: Nathan Grey [mailto:greynng@xxxxxxxxx]
> > Sent: 11 October 2013 16:20
> > To: Stuart Dallas
> > Cc: php-general@xxxxxxxxxxxxx
> > Subject: Re:  Trying to understand what is happening in this
> > code
> >
> > Stuart, Jose - Thanks for your quick response. Are you saying that
> > the
> > processor echos all the html tags it sees. Is it doing something
> > like this
> > to the script:
> >
> > echo <body>
> > echo <h1>The first twenty Fibonacci numbers:</h1>
> > echo <ul>
> >      <?php
> >        $first = 0;
> >        $second = 1;
> >        for ($i = 0; $i < 20; $i++) {
> >         ?>
> > echo  <li><?php echo $first + $second ?></li>
> >       <?php
> >         $temp = $first + $second;
> >         $first = $second;
> >         $second = $temp;
> >
> >       } ?>
> > echo  </ul>
> > echo  </body>
> >
> > Or is it just the line in question that is being echoed?
> >
> >
> > -----------------------------
> > Nathan Grey
> > 404-337-9005
> >
> >
> > On Fri, Oct 11, 2013 at 11:06 AM, Stuart Dallas <stuart@xxxxxxxx>
> > wrote:
> >
> > > On 11 Oct 2013, at 16:01, Nathan Grey <greynng@xxxxxxxxx> 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?
> > >
> > > Probably the easiest way to understand what's happening is to
> > assume that
> > > PHP converts anything in the file that's outside PHP tags in to
> > echo
> > > statements, and then executes the result. This isn't too far from
> > what
> > > actually happens.
> > >
> > > PHP compiles any script in to bytecodes, which it then executes.
> > Anything
> > > outside PHP tags does, essentially, get passed in to the echo
> > language
> > > construct.
> > >
> > > Does that help at all?
> > >
> > > -Stuart
> > >
> > > --
> > > Stuart Dallas
> > > 3ft9 Ltd
> > > http://3ft9.com/
> > >
>
>
> To view the terms under which this email is distributed, please go to
> http://disclaimer.leedsmet.ac.uk/email.htm
>
> --
> 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