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
}
?>
klye_g
  • 1,242
  • 6
  • 31
  • 54

3 Answers3

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.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • I already tried variable variables and no luck. This function is going to be existing inside a helper file. – klye_g Apr 25 '14 at 01:00
  • @K_G I forgot the `global` declaration, see the working example that I added. – jeroen Apr 25 '14 at 01:05
  • 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. – klye_g Apr 25 '14 at 01:08
  • @K_G Could be a scope problem, I am just checking the global scope. Hard to tell though without seeing it. – jeroen Apr 25 '14 at 01:09
  • I added a usage example above near the bottom of the question, hopefully this provides more insight. – klye_g Apr 25 '14 at 01:11
  • @K_G Your usage example does not make a lot of sense, the function in my answer returns a boolean and not the value of the variable. If you need that, you should probably rename your function, I'll add an example. – jeroen Apr 25 '14 at 01:13
  • you're right I should have called the function something different, my bad! – klye_g Apr 25 '14 at 01:15
  • the reason I am interested in making a helper function to do the work of a long if statement is to keep the view php cleaner and to obviously write a function that will erase the need for duplicate code and the second example only returns NULL. – klye_g Apr 25 '14 at 01:17
  • @K_G The `valueVar()` function should do what you need. – jeroen Apr 25 '14 at 01:19
  • 2
    Isn't `isset($$var) && !empty($$var)` equivalent to `!empty($$var)`? – Fabrício Matté Apr 25 '14 at 01:38
  • @FabrícioMatté Yes, using the two is just to illustrate what is happening but `empty()` does not trigger a warning when a variable does not exist so that's all you need. – jeroen Apr 25 '14 at 01:45
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);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Try:

<?php
function varIsset($string){
  global $$string;
  return empty($$string) ? 0 : 1;
}
$what = 'good';
echo 'what:'.varIsset('what').'; now:'.varIsset('now');
?>
StackSlave
  • 10,613
  • 2
  • 18
  • 35