How to echo a function only if variable has a value?

Discussion in 'PHP' started by Silvester Vella, Jul 20, 2016.

  1. #1
    How can I echo this function:
                <?php echo  "€" . get_post_meta( get_the_ID(), 'deal_sale_price', true ); ?>
    PHP:
    only if 'deal_sale_price' has a value?

    Thanks a lot. :)
     
    Silvester Vella, Jul 20, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Since this is a WP question, I'm not 100% sure this will work, but it might:
    
    <?php 
    if (!empty(get_post_meta(get_the_ID(),'deal_sale_price',true))) {
        echo "€" . get_post_meta( get_the_ID(), 'deal_sale_price', true ); 
    }
    ?>
    
    PHP:
     
    PoPSiCLe, Jul 20, 2016 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,803
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #3
    for readability, if you aren't too confident with PHP, you could go with this version of @PoPSiCLe's code

    <?php
    $deal_sale_price = get_post_meta(get_the_ID(),'deal_sale_price',true);
    if (!empty($deal_sale_price)) {
    echo "€" . $deal_sale_price;
    }
    ?>
    Code (markup):
     
    sarahk, Jul 20, 2016 IP
    ThePHPMaster likes this.
  4. Silvester Vella

    Silvester Vella Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Not confident with PHP at all, just starting to learn bits of it actually. Anyway Thanks a lot guys, both worked :):)
     
    Silvester Vella, Jul 21, 2016 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Legibility nothing, if you're gonna call a procedure more than once, STORE THAT PUPPY!

    I'd also advise against the string addition, "curly brackets for nothing", double quotes on a non-parse string.. and you could do the assignment and the check as a single operation.

    
    <?php
    
    if (!empty(
    	$deal_sale_price = get_post_meta(get_the_ID(),'deal_sale_price',true)
    )) echo '€', $deal_sale_price;
    
    ?>
    
    Code (markup):
    Remember, comma delimited echo is faster than string addition, uses less memory, and actually will run in the order you coded it.

    Just saying... goes with my general belief that 99% of the time people use string addition or double quotes on a string, they're doing something WRONG.
     
    deathshadow, Jul 24, 2016 IP
  6. acidsn0w

    acidsn0w Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    
    <?php
    
    function Check( $value )
    {
          if ( strlen($value) && is_string($value) )
          {
                return true;
          }
          else
          {
                return false;
          }
    }
    
    if Check(get_post_meta( get_the_ID(), 'deal_sale_price', true ))
        echo get_post_meta( get_the_ID(), 'deal_sale_price', true );
    
    ?>
    
    Code (markup):
     
    acidsn0w, Jul 28, 2016 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Why in the world would you create a separate function for this? *shakes head*
     
    PoPSiCLe, Jul 28, 2016 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #8
    ... and one with an if statement at that... Must just be a chubby chaser with a love of bloat like that.
     
    deathshadow, Jul 28, 2016 IP