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><?php echo $r->title; ?></h1> <div><?php echo $r->contents; ?> //why I can't use alternative syntax <?= $r->contents?> <?php endforeach; ?> // again why are we opening and closing PHP tag? So what confuses me is that normal PHP short tags should be like this: <?php foreach ($rows as $r) : // we are opening a foreach structure //dosomething endforeach; ?> // 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: <?php echo $variable; ?> With the alternative syntax you can instead do it this way: <?=$variable?> So in my case: <?php echo $r->contents; ?> <?= $r->contents?> 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 <?php ;?> tags to avoid errors later on. InsiteFX |