PHP接受字符串,并检查该字符串是否作为变量存在 [英] PHP take string and check if that string exists as a variable

查看:179
本文介绍了PHP接受字符串,并检查该字符串是否作为变量存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的情况.我正在使用一个包含在多个页面上的表单(为了简化并减少重复),并且该表单在某些区域中填充了数据库中的值.但是,并非所有这些值都将始终存在.例如,我可能正在做一些事情来达到以下效果:

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

,这将在存在值的地方很好地工作,但是$ user并不总是设置的,因为它们可能是第一次输入名称.是的,您可以在if语句(简写或常规)中执行isset($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)

我正在尝试编写一个辅助函数,以检查是否设置了变量以及变量是否不为null.理想情况下,我想做类似varIsset('first_name')的操作,其中first_name是实际的变量名$ first_name,该函数将接收字符串,将其转换为预期的变量$ first_name并检查其是否已设置且不为null.如果满足要求,则返回该变量值(在这种情况下为测试").如果未通过要求,即未设置要求或为null,则该函数将返回'{blank}'.

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}'.

如果有帮助,我正在使用CodeIgniter,它将在不久的将来切换到Laravel.任何帮助表示赞赏.到目前为止,这是我整理的内容,但无济于事.

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

以下是用法示例:

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

推荐答案

我会使用数组并自己检查数组键(或初始化我的所有变量...),但是对于您的函数,您可以使用类似以下内容的方法:

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

查看有关变量变量的手册.您需要使用global $$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.

编辑:如果您需要返回的值,则可以执行以下操作:

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.

这篇关于PHP接受字符串,并检查该字符串是否作为变量存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆