2

I am in need of a PHP function to check that a variable exist or not outside of function. If it does not exist, then assign a default value to it.

The function will be something like: function if_exist($argument, $default = '') where $argument will be any variable outside the function. It can be a variable's variable, and $default will be the default value of the variable if the variable doesn't exist.

Please avoid using global scope in the function, because I heard that it's not good for a site's security.

2
  • Using globals is not necessarily related to security (don't shout), but it makes the code much more difficult to understand and to maintain... Commented Jul 10, 2011 at 21:16
  • 1
    Global scope has little to nothing to do with security. Purists don't like global scope simply because they feel it pollutes the namespace and makes a program hard to maintain. If you don't abuse of it, you'll be fine. Commented Jul 10, 2011 at 21:19

5 Answers 5

5

You can make $argument pass-by-reference:

function if_exist(&$argument, $default="") {
   if(!isset($argument)) {
       $argument = $default;
       return false;
   }
   return true;
}

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

+1 because that's the only somewhat sensible thing to do. I don't think it's pretty or that it should be used, but rather relies on some of PHP's black magic.
@Andrew: That's not what I meant. The black magic is, that you can "pass an unidentified variable" without triggering some kind of error. but rather, PHP will silently initialize the variable in place with null.
For the record, your caps lock is on.
0

I don't see why you want a function for this at all.

$a = isset($a) ? $a : "my default string";

Everybody will understand what your code does without resorting to little-known "features" of PHP and having to look at your functions body to see what it does.

Even though it may look like a function call, it's not. isset() is a language construct! That's why it will not raise a Notice.

Comments

0

There are two ways of going at this. You may be looking for the first non-null value or if the actual variable is set or not...

The first use-case is easy... To get the value of the first variable that isn't null, define a function as such:

function coalesce() {
  $args = func_get_args();

  if(is_array($args))
    return null;

  foreach($args as $arg) {
    if($arg !== null)
      return $arg;
  }

  return null;
}

Using this function is easy. Simply call coalesce() with a number of different arguments and it will always return the first non-null value it encounters.

$myVar = coalesce($myVar, null, 'Hello There', 'World');
echo $myVar; // Hello there

However, if you are looking for a function to check if a variable is defined, and if it isn't define it, then you need to create a function which forces the first argument to be a reference.

function set_default(&$variable, $defaultValue = null) {
  if(!isset($variable)) {
    $variable = $defaultValue;

    return false;
  }
  return true;
}

1 Comment

No, isset() returns false when a variable is null. PS: You might want to name your function :) - the reason it must be a reference is, so PHP doesn't throw up on you and you can edit it of course while still having an effect in the calling scope.
0

Use this:

function setGlobalIfNotSet( $variableName, $defaultValue = false ) {

  if ( ! isset( $variableName )) {

    global $$variableName;

    $$variableName = $defaultValue;

  }

}

1 Comment

An explanation would be in order. E.g., why double sigils ($$, $$variableName)? Why would it compile?
-1

You can make a function to use isset:

function isset_default($v, $default = '') {
    return isset($v) ? $v : $default;
}

Usage:

$v = isset_default($v, 'somedefaultvalue');

1 Comment

Heh, oops. After I realized he wanted a function I knew my answer was going to be bad

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.