Hello,
I recently the following code on an online tutorial

<ol>
<?php foreach($todo as $item): ?>
<li><?=$item?> </li>
<?php endforeach;?>
</ol>

Could someone tell me why the list printed even though the whole thing is not contained within a single php bracket : <?php .... ?>. Thank you.

Hello,
I recently the following code on an online tutorial

<ol>
<?php foreach($todo as $item): ?>
<li><?=$item?> </li>
<?php endforeach;?>
</ol>

Could someone tell me why the list printed even though the whole thing is not contained within a single php bracket : <?php .... ?>. Thank you.

Do a search on: short_open_tag in the php manual.
It should take you to a page titled: "Description of core php.ini directives".

The short_open_tags; which is a configuration boolean in php.ini allows php tags to be defined as: <? ... ?> in addition to the standard <?php ... ?>.

In addition, it allows the use of the echo command's shortcut version. i.e. '<?=' where the equal sign denotes an echo command.

If you use XML in your php code, you'll probably want to set short_open_tag to 'off' in php.ini as it will conflict with using <?xml ?> inline.

Darrell

Short tags are a very nasty habit IMO and make reading/debugging the code more difficult.


Matti Ressler
Suomedia

Short tags are a very nasty habit IMO and make reading/debugging the code more difficult.


Matti Ressler
Suomedia

Matti is correct! Short tags make debugging/code review life a bear.
I never use short tags - especially since I use a lot of XML.

Darrell

I'm very sleepy and tired right now, so if i say something silly in the following lines please excuse me in advance...

I read the question and answers and i think you guys are missing the actual point. Because I understand, he actually asks why there are more than one php tag!!! ( am i being dull?!?)

So I'll answer the question in that sense...

Why list is printer even if the whole thing is not contained within a single php bracket?!

When you use any condition (if,for,foreach,while),you can close the php tag after you write the condition line then write some HTML in middle and then open an other tag to end the condition(endforeach). This is actually how your code is but you can also the do the following.

<ol>
<?php foreach($todo as $item):
 print "<li>".$item."</li>";
endforeach;
 ?>
</ol>

That would do exactly the same what your code does but this one would be harder to debug that's why it's always better to write everything else then php(html,js etc.) out of php tags.As your code does.

I'm very sleepy and tired right now, so if i say something silly in the following lines please excuse me in advance...

I read the question and answers and i think you guys are missing the actual point. Because I understand, he actually asks why there are more than one php tag!!! ( am i being dull?!?)

So I'll answer the question in that sense...

Why list is printer even if the whole thing is not contained within a single php bracket?!

When you use any condition (if,for,foreach,while),you can close the php tag after you write the condition line then write some HTML in middle and then open an other tag to end the condition(endforeach). This is actually how your code is but you can also the do the following.

<
ol>
<?php foreach($todo as $item):
 print "<li>".$item."</li>";
endforeach;
 ?>
</ol>

That would do exactly the same what your code does but this one would be harder to debug that's why it's always better to write everything else then php(html,js etc.) out of php tags.As your code does.

It doesn't make any difference, but I see what you mean.
Although the posts offered earlier still stand since the relevant areas are withing php tags - even the short tag.
The areas outside of the php tags will just be sent to the browser as raw html.

I notice you've changed the orginal code which now looks better and probably will run faster then the interpreter having to jump in and out (I'm unsure of the inner machinations of the interpreter, so don't quote me on that).

The original code:<li><?=$item?> </li>, just takes the variable $item and outputs the value since the variable's still in scope.

Darrell

Thanks everyone. Especially Free_Man, yes you understood my question. Could someone tell me the technical name for this feature. I would like to google it and learn more about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.