Discussion:
Trying to understand what is happening in this code
Nathan Grey
2013-10-11 15:01:11 UTC
Permalink
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
Stuart Dallas
2013-10-11 15:06:10 UTC
Permalink
Post by Nathan Grey
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
<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
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/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nathan Grey
2013-10-11 15:20:08 UTC
Permalink
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
Post by Nathan Grey
Post by Nathan Grey
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
Post by Nathan Grey
clarified the issue for me.
http://stackoverflow.com/questions/19309369/trying-to-understand-how-these-php-sections-work-together
Post by Nathan Grey
<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
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
Post by Nathan Grey
contained within a single pair of opening and closing tags for it to
work.
Post by Nathan Grey
2. Even if the processor is able to reassemble the loop seamlessly, how
do
Post by Nathan Grey
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/
Jose Nobile
2013-10-11 15:37:48 UTC
Permalink
Hi Nathan,

All content out of <?PHP ?> imagine like echo "..."
<?PHP
your php code ..
?>your html
html
html
html
<?PHP

your php code

?>


imagine as:

<?PHP
your php code ..
echo "your html
html
html
html";

your php code

?>


I post a answer in Stackoverlow :-)



Saludos,
José Nobile
Post by Nathan Grey
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
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
Post by Stuart Dallas
Post by Nathan Grey
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
Post by Nathan Grey
clarified the issue for me.
http://stackoverflow.com/questions/19309369/trying-to-understand-how-these-php-sections-work-together
Post by Stuart Dallas
Post by Nathan Grey
<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
Post by Stuart Dallas
Post by Nathan Grey
understand that HTML and PHP can be interspersed. I also understand
that
Post by Stuart Dallas
Post by Nathan Grey
the PHP processor will look over the code and pick up the code that is
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
Post by Stuart Dallas
Post by Nathan Grey
loop would be broken. I would expect that the entire loop would have to
be
Post by Nathan Grey
contained within a single pair of opening and closing tags for it to
work.
Post by Nathan Grey
2. Even if the processor is able to reassemble the loop seamlessly, how
do
Post by Nathan Grey
the <li> tags get included in the loop since they fall outside of the
PHP
Post by Stuart Dallas
Post by Nathan Grey
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/
Stuart Dallas
2013-10-11 15:39:25 UTC
Permalink
Post by Nathan Grey
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?
I'm not sure exactly what it gets compiled to, but I also don't see why it matters. All that matters is that content outside of PHP tags will simply get echo'd.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ford, Mike
2013-10-14 09:25:40 UTC
Permalink
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
-----Original Message-----
Sent: 11 October 2013 16:20
To: Stuart Dallas
Subject: Re: [PHP] 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
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
Post by Nathan Grey
Post by Nathan Grey
Hi - I am new to PHP so this is probably a very rudimentary
question. I
Post by Nathan Grey
Post by Nathan Grey
posed it over at Stack Overflow and got several responses but
none of
Post by Nathan Grey
them
Post by Nathan Grey
clarified the issue for me.
http://stackoverflow.com/questions/19309369/trying-to-understand-
how-these-php-sections-work-together
Post by Nathan Grey
Post by Nathan Grey
<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
Post by Nathan Grey
Post by Nathan Grey
understand that HTML and PHP can be interspersed. I also
understand that
Post by Nathan Grey
Post by Nathan Grey
the PHP processor will look over the code and pick up the code
that is
Post by Nathan Grey
Post by Nathan Grey
between the opening and closing PHP tags. Here's where I am
1. Can the processor really reassemble a for-loop that is broken
into
Post by Nathan Grey
Post by Nathan Grey
segments by opening and closing tags? It seems like the
integrity of the
Post by Nathan Grey
Post by Nathan Grey
loop would be broken. I would expect that the entire loop would
have to
Post by Nathan Grey
be
Post by Nathan Grey
contained within a single pair of opening and closing tags for
it to
Post by Nathan Grey
work.
Post by Nathan Grey
2. Even if the processor is able to reassemble the loop
seamlessly, how
Post by Nathan Grey
do
Post by Nathan Grey
the <li> tags get included in the loop since they fall outside
of the PHP
Post by Nathan Grey
Post by Nathan Grey
tags?
Probably the easiest way to understand what's happening is to
assume that
Post by Nathan Grey
PHP converts anything in the file that's outside PHP tags in to
echo
Post by Nathan Grey
statements, and then executes the result. This isn't too far from
what
Post by Nathan Grey
actually happens.
PHP compiles any script in to bytecodes, which it then executes.
Anything
Post by Nathan Grey
outside PHP tags does, essentially, get passed in to the echo
language
Post by Nathan Grey
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
Nathan Grey
2013-10-14 18:30:15 UTC
Permalink
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
Post by Ford, Mike
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
-----Original Message-----
Sent: 11 October 2013 16:20
To: Stuart Dallas
Subject: Re: [PHP] 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
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
Post by Nathan Grey
Post by Nathan Grey
Hi - I am new to PHP so this is probably a very rudimentary
question. I
Post by Nathan Grey
Post by Nathan Grey
posed it over at Stack Overflow and got several responses but
none of
Post by Nathan Grey
them
Post by Nathan Grey
clarified the issue for me.
http://stackoverflow.com/questions/19309369/trying-to-understand-
how-these-php-sections-work-together
Post by Nathan Grey
Post by Nathan Grey
<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
Post by Nathan Grey
Post by Nathan Grey
understand that HTML and PHP can be interspersed. I also
understand that
Post by Nathan Grey
Post by Nathan Grey
the PHP processor will look over the code and pick up the code
that is
Post by Nathan Grey
Post by Nathan Grey
between the opening and closing PHP tags. Here's where I am
1. Can the processor really reassemble a for-loop that is broken
into
Post by Nathan Grey
Post by Nathan Grey
segments by opening and closing tags? It seems like the
integrity of the
Post by Nathan Grey
Post by Nathan Grey
loop would be broken. I would expect that the entire loop would
have to
Post by Nathan Grey
be
Post by Nathan Grey
contained within a single pair of opening and closing tags for
it to
Post by Nathan Grey
work.
Post by Nathan Grey
2. Even if the processor is able to reassemble the loop
seamlessly, how
Post by Nathan Grey
do
Post by Nathan Grey
the <li> tags get included in the loop since they fall outside
of the PHP
Post by Nathan Grey
Post by Nathan Grey
tags?
Probably the easiest way to understand what's happening is to
assume that
Post by Nathan Grey
PHP converts anything in the file that's outside PHP tags in to
echo
Post by Nathan Grey
statements, and then executes the result. This isn't too far from
what
Post by Nathan Grey
actually happens.
PHP compiles any script in to bytecodes, which it then executes.
Anything
Post by Nathan Grey
outside PHP tags does, essentially, get passed in to the echo
language
Post by Nathan Grey
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
Jose Nobile
2013-10-11 15:07:39 UTC
Permalink
Hi,

Just imagine that HTML is printed using echo "<html ...", it not matter
when or where you open or close php, its transparent to code execution.


Saludos,
José Nobile
Post by Nathan Grey
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
<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
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
Marc Guay
2013-10-11 15:12:44 UTC
Permalink
Here's another way to write a loop that might make it more clear
what's happening...

<ul>
<?php foreach ($weekdays as $day) : ?>
<li>
<?php echo $day; ?>
</li>
<?php endforeach;?>
</ul>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nathan Grey
2013-10-11 16:15:24 UTC
Permalink
Okay, this helps because it is even more mystifying. But actually, I think
I finally get it. You are like a Zen master, Marc.

It's probably not technically correct but when I look at this I think that
PHP turns HTML from a markup language into a scripting language. Okay,
maybe the way to say it is that PHP is a scripting language that integrates
seamlessly into HTML. Basically, the combination of PHP/HTML results in a
scripting language with markup features. Oneness. Wow.


-----------------------------
Nathan Grey
404-337-9005
Post by Marc Guay
Here's another way to write a loop that might make it more clear
what's happening...
<ul>
<?php foreach ($weekdays as $day) : ?>
<li>
<?php echo $day; ?>
</li>
<?php endforeach;?>
</ul>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Marc Guay
2013-10-11 16:25:28 UTC
Permalink
Nathan, now that you've seen the light, I pray that you never have to
use Smarty.

(If you continue on in web dev you will know what I mean soon enough).

Marc
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Shawn McKenzie
2013-10-11 17:10:17 UTC
Permalink
+1
Post by Marc Guay
Nathan, now that you've seen the light, I pray that you never have to
use Smarty.
(If you continue on in web dev you will know what I mean soon enough).
Marc
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stuart Dallas
2013-10-11 17:18:26 UTC
Permalink
Sorry for the top post.




I don't see what's so confusing. It's not magic, it's a simple mechanism where anything not in PHP tags gets echo'd. There're is not conversion of the content outside PHP tags, and thinking there is will likely make it more confusing.




If you're asking because you want to know how it's actually implemented then you can read the source code or ask on the internals list. It appears to me that you're trying to make it more complicated than it really is or needs to be.




-Stuart




-- 

Stuart Dallas

3ft9 Ltd

http://3ft9.com/

—
Sent from Mailbox for iPhone
Post by Nathan Grey
Okay, this helps because it is even more mystifying. But actually, I think
I finally get it. You are like a Zen master, Marc.
It's probably not technically correct but when I look at this I think that
PHP turns HTML from a markup language into a scripting language. Okay,
maybe the way to say it is that PHP is a scripting language that integrates
seamlessly into HTML. Basically, the combination of PHP/HTML results in a
scripting language with markup features. Oneness. Wow.
-----------------------------
Nathan Grey
404-337-9005
Post by Marc Guay
Here's another way to write a loop that might make it more clear
what's happening...
<ul>
<?php foreach ($weekdays as $day) : ?>
<li>
<?php echo $day; ?>
</li>
<?php endforeach;?>
</ul>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-10-12 05:41:38 UTC
Permalink
Post by Stuart Dallas
Sorry for the top post.
I don't see what's so confusing. It's not magic, it's a simple mechanism where anything not in PHP tags gets echo'd. There're is not conversion of the content outside PHP tags, and thinking there is will likely make it more confusing.
If you're asking because you want to know how it's actually implemented then you can read the source code or ask on the internals list. It appears to me that you're trying to make it more complicated than it really is or needs to be.
-Stuart
I think it can be confusing to newbies because it looks like a context
switch when you move in or out of PHP tags. Thus to a green eye it might
seem like previous contexts are disrupted (as his comment suggests when
he thought the loop would be broken when switching out of PHP).

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David OBrien
2013-10-11 17:24:48 UTC
Permalink
This explains it also: http://us1.php.net/manual/en/faq.html.php
Jim Giner
2013-10-11 17:52:57 UTC
Permalink
Post by Nathan Grey
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
<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
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-10-12 05:43:09 UTC
Permalink
Post by Jim Giner
Post by Nathan Grey
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
<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
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.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-10-12 13:03:30 UTC
Permalink
Post by Robert Cummings
Post by Jim Giner
Post by Nathan Grey
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
<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
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
Continue reading on narkive:
Loading...