3

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

8
  • 1
    What do you mean by "respected"? Commented Jun 28, 2017 at 8:55
  • 3
    The problem with your idea of creating a function, is that a function has to be called, and the variable passed to that function. If the variable does not exist, then when you use its name in the function call, IT WILL CAUSE AN ERROR, because it does not exist! Commented Jun 28, 2017 at 8:57
  • 1
    just two tips for ya: keep {} on same line - there isn't necessarily strict PHP standards but a standard has been slowly building. Secondly AND can be shortened to && Commented Jun 28, 2017 at 8:57
  • Thanks guys, i was thinking about no displaying the erroe, but thats avoiding the problem. @Swellar i meant the condition is respected if $myvariable == "something" is true Commented Jun 28, 2017 at 9:00
  • 1
    @ThisGuyHasTwoThumbs If I'm not mistaken, what Riggs meant is that if the passed variables have not been checked whether it exists, it will cause an error when you call IssetCond and not because of the variable's scope Commented Jun 28, 2017 at 9:03

3 Answers 3

2

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';
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Your idea it good but if you pass a variable to your function you pass the value of that variable to your function. If your variable is not set before you get an error that you try to prevent with your function.

You could pass the reference to the function but that would cause the same problem if you variable is not set before.

Comments

0

I think i found the answer, thanks to @Teko

   $a = "3";
   function IssetCond($var, $value){
    global $$var;
    if (isset($$var) && $$var == $value)
    {   return true;}
    return false;
 }
if(IssetCond('d', 4))
{
    echo 'first test'; // wont display
}
if(IssetCond("a", 3))
{
    echo 'second test'; // will display
}
if(IssetCond("a", 4))
{
    echo 'third test'; // wont display
}

Thanks to everyone !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.