Hello All, I have been getting into some heavy PHP coding lately and could not figure out when to use the endif statement. Here is my current code: <?php if ($_product->isGrouped()) { ?> <?php $_description = $this->getProduct()->getDescription(); ?> <?php if ($_description){ ?> <h2><?php echo $this->__('Details') ?></h2> <div class="std"> <?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?> </div> <?php } endif; ?> <?php } else { ?> <?php } endif; ?> PHP: I know this is not right at all. Does anyone mind helping me out and showing me where the endifs would go and explain why? Best Regards, Nick
PHP endif does not require brackets. For example: if ($var == 'something'): //Do stuff endif; PHP: The way you've written it - with brackets - does not need endif. And why are you opening and closing php that much? Here's how I'd write that: <?php if($_product->isGrouped()) { $_description = $this->getProduct()->getDescription(); if($_description){ echo '<h2>'.$this->__('Details').'</h2> <div class="std"> '.$this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description').' </div>'; } } else { //If product is not grouped. } ?> PHP: Or if you want to use alternative syntax stucture: <?php if($_product->isGrouped()): $_description = $this->getProduct()->getDescription(); if($_description): echo '<h2>'.$this->__('Details').'</h2> <div class="std"> '.$this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description').' </div>'; endif; else: //If product is not grouped. endif; ?> PHP: That way PHP is only run once in the file, slightly increasing performance and more importantly, greatly improving readability.
Its normally used by MVC programmers in the view part rather than printing or echoing out all the code. Supposed to make it cleaner and easier to read.
It doesn't though, it makes it less efficient (due to constant starting and finishing php) and even harder to read. MVC is good in concept but not entirely applicable, especially in PHP. Even if you didn't want to echo all the content at once, opening and closing new PHP tags each line is pretty redundant: <?php if ($_product->isGrouped()) { ?> <?php $_description = $this->getProduct()->getDescription(); ?> <?php if ($_description){ ?> PHP: instead of <?php if($_product->isGrouped()) { $_description = $this->getProduct()->getDescription(); if ($_description){ ?> PHP: just seems highly illogical and messy to me.
That's so true! The code like your first example used to be (still is?) in the default wordpress theme and would drive me nuts. I could see what they were trying to do but it just made everything so much messier.
"Supposed to". But you're "supposed to" keep the code in the code part and the display in the display part. Doing it this way - unless you're writing for a target like people running WordPress - defeats the whole MVC concept. "I'll keep my programming code and my display code separate, but I'll stick programming code into my display code." When there's no good comedy on TV, and you need a good laugh, some code you can find on the internet provides the laughter. It allows one to copy a line from a "programming WordPress" site and paste it into the core code - and have any hope that it will work. But I agree - writing code for WP does resemble squirrel food.
You can still get logic in the display part. If user_status = abc then show xyz If content_status = def then give a link to the edit page
if/endif does not mix with curly braces ({}), it's best used with html for template purposes.. <?php if($user == 1): ?> <a href="logout.php">Logout</a> <?php else: ?> <a href="login.php">Login</a> <?php endif; ?> PHP:
Which is why as a rule of thumb I consider ANY .php file that has more than one <?php ?> pairing in the code to be total half-assed rubbish in desperate need of a rewrite. It goes hand in hand with the half-assed code people vomit up where they use double quotes on strings for no reason... things which to be frank I think should be removed entirely from the PHP specification as a waste of time and only encouraging bad coding practices... (along with the mysql_ functions and the procedural versions of mysqli_) Opening and closing it on every LINE? Every time I see someone doing that I feel the urge to break out the 1970's style pimp slap. It's just more proof that these folks shoe-horning the MVC concept into PHP don't actually understand what MVC is, or even what good programming practices are. Oh, and guys, STOP using string additions on echo... comma delimited runs faster and if that's a method call, the method will output BEFORE the code it's in! function test() { echo 'Test'; } echo 'This is a '.test().'!<br />'; echo 'This is a ',test(),'!<br />'; Code (markup): Will output: TestThis is a ! This is a Test! No joke. String additions on echo should only be used inside inlined conditionals. I suspect that's where a lot of the wasteful use of <?php and ?> comes from as this: This is a <?php test(); ?>! would also output as expected -- and usually the code written like that is chock full of string additions for no reason when they do use echo/print.
The beauty about PHP is that there are more than one way of doing things - you are not restricted to one like other languages. Nothing of them are wrong, but some are just more appropriate to use than the other.