PHP:在未警告的情况下打印未定义的变量 [英] PHP: printing undefined variables without warning

查看:203
本文介绍了PHP:在未警告的情况下打印未定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有一种快速的方法来回显未定义的变量而不发出警告? (我可以更改错误报告级别,但我不想更改.)到目前为止,我最小的是:

I'm just wondering if there is a quick way to echo undefined variables without getting a warning? (I can change error reporting level but I don't want to.) The smallest I have so far is:

isset($variable)?$variable:''

我不喜欢这样做有几个原因:

I dislike this for a few reasons:

  • 有点罗word"和复杂
  • $variable被重复
  • 空白字符串的回声总是让我很烦.
  • 我的变量名可能会更长,例如$arrayvar['parameter']
  • It's a bit "wordy" and complex
  • $variable is repeated
  • The echoing of a blank string always kind of annoys me.
  • My variable names will probably be longer, eg $arrayvar['parameter']

推荐答案

您可以使用取自这里:

function ifsetor(&$variable, $default = null) {
    if (isset($variable)) {
        $tmp = $variable;
    } else {
        $tmp = $default;
    }
    return $tmp;
}

例如:

echo ifsetor($variable);
echo ifsetor($variable, 'default');

这不会产生通知,因为该变量是通过引用传递的.

This does not generate a notice because the variable is passed by reference.

这篇关于PHP:在未警告的情况下打印未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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