1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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,624
    Likes Received:
    4,486
    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 Member

    Messages:
    284
    Likes Received:
    22
    Best Answers:
    3
    Trophy Points:
    33
    #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,194
    Likes Received:
    1,670
    Best Answers:
    29
    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,194
    Likes Received:
    1,670
    Best Answers:
    29
    Trophy Points:
    475
    #5
    if($payment > 0) {
    ... do this
    }

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