CodeIgniter Forums
Alternative Control Structures - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Alternative Control Structures (/showthread.php?tid=34171)



Alternative Control Structures - El Forum - 09-21-2010

[eluser]Milos[/eluser]
Hi all.

I just started to use CodeIgniter so I can't understand something very basic.

I am doing tutorials...

Anyway code looks like this: (this is from a view "home.php")


<?php foreach ($rows as $r) : ?> //why are we closing PHP tag here?

<h1>&lt;?php echo $r->title; ?&gt;</h1>
<div>&lt;?php echo $r->contents; ?&gt; //why I can't use alternative syntax &lt;?= $r->contents?&gt;

&lt;?php endforeach; ?&gt; // again why are we opening and closing PHP tag?

So what confuses me is that normal PHP short tags should be like this:

&lt;?php foreach ($rows as $r) : // we are opening a foreach structure

//dosomething

endforeach; ?&gt; // we are closing a foreach structure

So this makes for me a second problem to understand echo statement used inside.

I didn't find so many things to read about this.

Also I wander what is the code in CodeIgniter itself that allows this behavior, since if I look at it I may understand this better.

Thanks


Alternative Control Structures - El Forum - 09-21-2010

[eluser]Eric Barnes[/eluser]
This is php itself http://php.net/manual/en/control-structures.alternative-syntax.php


Alternative Control Structures - El Forum - 09-21-2010

[eluser]Milos[/eluser]
Thanks.

Still I don't understand why can't I echo like this:

Normally to echo, or print out a variable you would do this:
&lt;?php echo $variable; ?&gt;

With the alternative syntax you can instead do it this way:
&lt;?=$variable?&gt;

So in my case:

&lt;?php echo $r->contents; ?&gt;

&lt;?= $r->contents?&gt; don't work.


Alternative Control Structures - El Forum - 09-21-2010

[eluser]danmontgomery[/eluser]
Then you don't have short tags enabled in php.ini


Alternative Control Structures - El Forum - 09-21-2010

[eluser]Milos[/eluser]
:O)

I was stupid.

Thank you.


Alternative Control Structures - El Forum - 09-21-2010

[eluser]InsiteFX[/eluser]
You should not use the short tags, because some servers
have turned them off. stick to using &lt;?php ;?&gt; tags
to avoid errors later on.

InsiteFX