Welcome Guest, Not a member yet? Register   Sign In
Alternative Control Structures
#1

[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
#2

[eluser]Eric Barnes[/eluser]
This is php itself http://php.net/manual/en/control-structu...syntax.php
#3

[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.
#4

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

[eluser]Milos[/eluser]
:O)

I was stupid.

Thank you.
#6

[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




Theme © iAndrew 2016 - Forum software by © MyBB