PHP take string and check if that string exists as a variable

淺唱寂寞╮ 提交于 2019-12-01 21:51:20

问题


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
}
?>

回答1:


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.




回答2:


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);



回答3:


Try:

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


来源:https://stackoverflow.com/questions/23282389/php-take-string-and-check-if-that-string-exists-as-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!