pwired Posted May 21, 2014 Share Posted May 21, 2014 Hi, I found this code inside a template of a webshop. what is this php code doing ? <?php }?> There is no echo and no print so what is this } character doing there. I´m sure there is a good reason but my php skills lack to know the right answer. Link to comment Share on other sites More sharing options...
apeisa Posted May 21, 2014 Share Posted May 21, 2014 It's ugly stuff that probably ends some if { from earlier. 4 Link to comment Share on other sites More sharing options...
thetuningspoon Posted May 21, 2014 Share Posted May 21, 2014 This is common when going in and out of php while coding. When doing this you can avoid having to use echo/print. For example: <?php if($foo = $bar) { ?> <p>Show this text and this <?= $variable ?>.</p> <?php } else { ?> <p>Show this text instead with this <?= $otherVariable ?>.</p> <?php } ?> I like to keep my open and close php tags on separate lines to keep them out of the way so they don't mess with my indentation. But you'll see here that the same <?php } ?> tag appears at the bottom to close out the if/else statement. I prefer to code this way because it puts the markup (html) first rather than the php. And you can avoid having to put all of your html in quotes and using escape characters or single quotes where you have conflicting quotes in your html. Plus indentation is easier, and is preserved when you view source later on. I use the shortened <? tag instead of <?php to make it even cleaner (although I've heard some servers don't like that-- I haven't run into any issues yet). Link to comment Share on other sites More sharing options...
apeisa Posted May 21, 2014 Share Posted May 21, 2014 I think most of us do that, at least sometimes. But there are no things in coding I hate more than trying to figure out where does lonely } starts. Well, there is. Add few more ifs inside each others, and the soup is ready. When you have huge chunks of html, maybe few intended ifs and then some lonely <?php } ?> or <?php endif; ?>, you are far from readable and easily manageable code. So my preference is actually: you should have so little logic on your markup generation, that echoing the parts that go inside ifs is good (usually oneliners). When having bigger chunks, then you should have other means: using functions, includes, some object or moving more logic to controller (or using actual controller). // with little html, echoing is much cleaner (imo) if($foo == $bar) { echo "<p>Show this text and this $variable</p>"; } else { echo "<p>Show this text instead with this $otherVariable</p>"; } // functions if($foo == $bar) { echo renderStuff1(); } else { echo renderStuff2(); } // includes if($foo == $bar) { include(./markup/stuff1.inc); } else { include(./markup/stuff2.inc); } // object $markup = new Markup(); if($foo == $bar) { echo $markup->stuff1(); } else { echo $markup->stuff2(); } 5 Link to comment Share on other sites More sharing options...
pwired Posted May 21, 2014 Author Share Posted May 21, 2014 Thanks for the replies, I learned a next step into php. 1 Link to comment Share on other sites More sharing options...
bwakad Posted May 27, 2014 Share Posted May 27, 2014 This is probably why I go over my (same) code every day. Just to make the same with less code as possible. In some parts of my code I prefer switch(). More easy to read then IF and no need to use {} to much. But depends on what I want of course. Link to comment Share on other sites More sharing options...
Recommended Posts