Using the PHP Endif

Discussion in 'PHP' started by Borduhh, Oct 14, 2011.

  1. #1
    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
     
    Last edited: Oct 14, 2011
    Borduhh, Oct 14, 2011 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    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.
     
    Last edited: Oct 14, 2011
    blueparukia, Oct 14, 2011 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #3
    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.
     
    sarahk, Oct 14, 2011 IP
  4. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #4
    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.
     
    blueparukia, Oct 14, 2011 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #5
    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.
     
    sarahk, Oct 15, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    "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.
     
    Rukbat, Oct 16, 2011 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #7
    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
     
    sarahk, Oct 16, 2011 IP
  8. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #8
    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:
     
    JohnnySchultz, Oct 17, 2011 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    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.
     
    Last edited: Oct 17, 2011
    deathshadow, Oct 17, 2011 IP
  10. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #10
    Wow it's been a while.
     
    blueparukia, Oct 17, 2011 IP
  11. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #11
    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.
     
    Rainulf, Oct 17, 2011 IP