Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pHp function - testing if my variable exists and if a condition is respected

i would like to create a php function. Basically, that's what i've :

if(isset($myvariable) AND $myvariable == "something")
{
 echo 'You can continue';
}

and that's what i want :

if(IssetCond($myvariable, "something"))
{
 echo 'You can continue';
}

I want function that tests if the variable exists, and if it does it tests if the condition is respected. Otherwise it returns false.

I've tried a lot of things, but i still have a problem when $myvariable doesnt exist.

function IssetCond($var, $value){
    if(isset($var) AND $var == $value)
    {
        return true;
    }
    else
   {
        return false;
   }
}

When $myvariable exists and whether the condition is respected or not, it works.

If you have some minutes to help me i'll be grateful.

Thanks

Thomas

like image 832
T. Moulin Avatar asked Apr 26 '26 13:04

T. Moulin


1 Answers

You can pass just the name, not the var, and use variable variable

function IssetCond($var, $value){
    return (isset($$var) AND $var == $value);
 }

Used like

 if(IssetCond('myvariable', "something"))
 {
     echo 'You can continue';
 }
like image 176
Teko Avatar answered Apr 28 '26 03:04

Teko



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!