Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if two vars are set?

Tags:

php

Depending on whether 2 vars are set ($skip and $take) I want to do different things. I have a large if else statement, is there a more efficient way to write this?

    if (isset($skip) && !isset($take)) {
        //skip only
    } elseif (!isset($skip) && isset($take)) {
        //take only
    } elseif (isset($skip) && isset($take)) {
        //skip and take
    } else {
        //default
    }

Edit

It should also be noted that this is to sit in a method where the vars will be set to null if not specified:

getAll($skip = null, $take = null)
like image 941
panthro Avatar asked Feb 16 '26 14:02

panthro


1 Answers

You can simplify the logic a bit:

if (isset($skip) && isset($take)) {
    // skip and take
} elseif (isset($skip)) {
    // only skip
} elseif (isset($take)) {
    // only take
} else {
    // default 
}
like image 68
Israel Unterman Avatar answered Feb 19 '26 02:02

Israel Unterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!