4

I have an interesting situation. I am using a form that is included on multiple pages (for simplicity and to reduce duplication) and this form in some areas is populated with values from a DB. However, not all of these values will always be present. For instance, I could be doing something to the effect of:

<?php echo set_value('first_name', $first_name); ?>

and this would work fine where the values exist, but $user is not always set, since they may be typing their name in for the first time. Yes you can do isset($first_name) && $first_name inside an if statement (shorthand or regular)

I am trying to write a helper function to check if a variable isset and if it's not null. I would ideally like to do something like varIsset('first_name'), where first_name is an actual variable name $first_name and the function would take in the string, turn it into the intended variable $first_name and check if it's set and not null. If it passes the requirements, then return that variables value (in this case 'test'). If it doesn't pass the requirements, meaining it's not set or is null, then the function would return '{blank}'.

I am using CodeIgniter if that helps, will be switching to Laravel in the somewhat near future. Any help is appreciated. Here is what I've put together so far, but to no avail.

function varIsset($var = '')
{   
    foreach (get_defined_vars() as $val) {
        if ($val == $var) {
            if (isset($val) && $val) {
                echo $val;
            }
            break;
        }
    }
    die;
}

Here is an example usage:

<?php 
if (varIsset('user_id') == 100) {
    // do something
}
?>

3 Answers 3

2

I would use arrays and check for array keys myself (or initialize all my variables...), but for your function you could use something like:

function varIsset($var)
{   
    global $$var;
    return isset($$var) && !empty($$var);
}

Check out the manual on variable variables. You need to use global $$var; to get around the scope problem, so it's a bit of a nasty solution. See a working example here.

Edit: If you need the value returned, you could do something like:

function valueVar($var)
{   
    global $$var;
    return (isset($$var) && !empty($$var)) ? $$var : NULL;
}

But to be honest, using variables like that when they might or might not exist seems a bit wrong to me.

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

11 Comments

I already tried variable variables and no luck. This function is going to be existing inside a helper file.
@K_G I forgot the global declaration, see the working example that I added.
thanks for the working example. I am still getting a FALSE though, and I know the variable is most definitely set in the controller -> view that I am looking at.
@K_G Could be a scope problem, I am just checking the global scope. Hard to tell though without seeing it.
Isn't isset($$var) && !empty($$var) equivalent to !empty($$var)?
|
0

It would be a better approach to introduce a context in which you want to search, e.g.:

function varIsset($name, array $context)
{
    return !empty($context[$name]);
}

The context is then populated with your database results before rendering takes place. Btw, empty() has a small caveat with the string value "0"; in those cases it might be a better approach to use this logic:

return isset($context[$name]) && strlen($name);

Comments

0

Try:

<?php
function varIsset($string){
  global $$string;
  return empty($$string) ? 0 : 1;
}
$what = 'good';
echo 'what:'.varIsset('what').'; now:'.varIsset('now');
?>

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.