5

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:

  • 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']
2
  • Really not a good idea. If that's seems to be "It's a bit "wordy" and complex", then you'll have some surprise in the future... There is no way this going to be you app bottleneck. Start using the good habit of checking var. Make you own function to speed up the process if you like. Commented Dec 19, 2008 at 10:32
  • @esatis: I'm not talking about speed at all... Commented Jul 10, 2009 at 12:20

7 Answers 7

15

You can run it with the error suppression operator @.

echo @$variable;

However, it's best not to ignore unset variables. Unset variables could indicate a logical error on the script, and it's best to ensure all variables are set before use.

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

3 Comments

The one exception I usually find myself making to this rule is unset array indexes. These can crop up all over the place. It's also better to put it on the variable itself as @$variable, so that it only supresses the unset error.
Yeah thanks for that. I almost never use this operator for the reasons I mentioned :)
Fully agree with the suggestion that this is not best practice.
9

you could use the ifsetor() example taken from here:

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

for example:

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

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

2 Comments

iirc this will still produce the error, since php is sending an undefined variable into a function, even though the isset is within it.
It won't, because it is passed by reference.
4
echo @$variable;

Comments

1

This is a long-standing issue with PHP, they intend to fix it with isset_or() (or a similar function) in PHP 6, hopefully that feature will make it into PHP 5.3 as well. For now, you must use the isset()/ternary example in your question, or else use the @ prefix to silence the error. IMHO, this is the only circumstance that justifies using @ in PHP.

I wouldn't worry about speed issues using echo with an empty string, it is probably more expensive to wrap it in an if clause than to just echo empty string.

1 Comment

I find @ to be useful when doing database connections or file operations where a failure would show errors. eg: "if ($fh = @fopen('myfile', 'w'))" - it lets you gracefully handle any problems.
1

You can use ?? selector for PHP 7 and later.

echo $variable??'not defined';

Comments

0

undefined variables are very common, i suggest you to initialize variable with null at first

$var = null;

or disable error reporting for notices:

error_reporting(E_ALL^E_NOTICE);

Comments

-1

Suppress errors using the @-operator forces the interpreter to change error level, executing the function and then change back error level. This decreases your scripts runtime.

Build a function like this will eliminate at least 3 of your reasons:

function echoVar($var, $ret=NULL) {
    return isset($var)?$var:$ret;
}

echoVar($arrayvar['parameter']);

But why echoing undefined variables? This sounds like not really well coded...

1 Comment

I think echoVar needs to accept $var by reference, in order to not show an error.

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.