Is negative variable equals empty?

Discussion in 'PHP' started by qwikad.com, Sep 29, 2024.

  1. #1
    if($payment) {
    ... do this
    }

    What if $payment is -5 will it be considered empty?
     
    qwikad.com, Sep 29, 2024 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,790
    Likes Received:
    4,529
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I tried this in a sandbox and -5 isn't empty

    <?php
    
    function isItEmpty($num){
        echo (empty($num)? 'yes': 'no')."\n";
    }
    $tot = 100;
    echo isItEmpty(10);
    echo isItEmpty(0);
    echo isItEmpty(-5);
    echo isItEmpty('10');
    echo isItEmpty('0');
    echo isItEmpty('-5');
    echo isItEmpty('');
    
    Code (markup):
    results
    no
    yes
    no
    no
    yes
    no
    yes
    Code (markup):
     
    sarahk, Sep 29, 2024 IP
    qwikad.com likes this.
  3. GreenHost.Cloud

    GreenHost.Cloud Active Member

    Messages:
    471
    Likes Received:
    34
    Best Answers:
    3
    Trophy Points:
    73
    #3
    In the context of PHP, a negative variable like $payment = -5 is not considered empty, it's still holding a value. The condition `if($payment)` will be evaluated as true because non-zero numbers are treated as true. So, it would execute the code inside the block.
     
    GreenHost.Cloud, Sep 30, 2024 IP
    qwikad.com likes this.
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,265
    Likes Received:
    1,693
    Best Answers:
    31
    Trophy Points:
    475
    #4
    When I do a refund, one payment app I'm using, sends it through the same ipn. I'm trying to figure out how I'd tell the script in the ipn file to ignore the refunds (in my instance, negative payments).
     
    qwikad.com, Sep 30, 2024 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,265
    Likes Received:
    1,693
    Best Answers:
    31
    Trophy Points:
    475
    #5
    if($payment > 0) {
    ... do this
    }

    works.
     
    Last edited: Oct 1, 2024
    qwikad.com, Oct 1, 2024 IP
    sarahk likes this.