Search code examples
phposclass

PHP - Logical operator with IF/Else statement test not working correctly


Code from: oc-content/themes/realestate/item.php

I have this piece of PHP code which is part of a 'framework' called OSClass. Basically, I am trying to 'hide' the user (the publisher of the advert) name to non-logged in users. In other words, only if a user is logged in, only then they can see if the publisher users advert.

I found out that I need to add this piece of code osc_is_web_user_logged_in(), which has worked perfectly for another section, however, due to the else statement, the publishers name is still displaying...

How can I amend the else statement? I could remove the else statement, but I'm worried it will 'break' something and I am unsure what osc_item_user_id() does... Do I add another if statement within the else statement (complete PHP newbie here).

<div class="ico-author ico"></div>                  
    <?php if( osc_item_user_id() != null && osc_is_web_user_logged_in()  ){ ?> 
          <?php echo osc_item_contact_name(); ?>
    <?php } else { ?>
          <?php echo osc_item_contact_name(); ?>
    <?php } ?>

Thanks!


Solution

  • In my opinion you should simple write:

    <div class="ico-author ico"></div>                  
        <?php if( osc_item_user_id() != null && osc_is_web_user_logged_in()  ){ ?> 
              <?php echo osc_item_contact_name(); ?>
        <?php } ?>
    

    As you don't want to display publisher name when user is not logged, you don't need have else section.