fahim uddin
fahim uddin

Reputation: 1

I wrote a function in php but somehow it fails

function gp_barrier_check($amount) {
    $return = 1;

    if ($amount == 11  OR $amount == 12  OR $amount == 18  OR $amount == 35  OR
        $amount == 58  OR $amount == 75  OR $amount == 81  OR $amount == 114 OR
        $amount == 115 OR $amount == 288 OR $amount == 317 OR $amount == 344 OR 
        $amount == 403 OR $amount == 574 OR $amount == 804 OR $amount == 1093)
    {
        $return = 0;
    }

    return $return;
}

It works fine but it fails once when send 18. I don't know why function failed.

Upvotes: 0

Views: 58

Answers (2)

Works for a Living
Works for a Living

Reputation: 1293

Nathan's answer is fine but it's going to return true if the amount is found in the array, whereas the OP wants to return 0 in such a case. Thus:

function gp_barrier_check($amount) 
{
    return (int)!in_array((int)$amount, array(11,12,18,35,58,75,81,114,115,288,317,344,403,574,804,1093));
}

Upvotes: 1

Nathan Dawson
Nathan Dawson

Reputation: 19308

There's no reason your function shouldn't work providing it is used correctly however as users have pointed out in the comments section, the approach taken is less than ideal. My suggestion would be the following:

function gp_barrier_check( $amount ) {
    return (int) in_array( $amount, array( 11, 12, 18, ) );
}

I'm casting the return value as an integer to match your original question although it seems likely that you'll want a boolean in which case just remove (int).

Upvotes: 0

Related Questions