A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP: printing undefined variables without warning - page 1
User InfoPosts
PHP: printing undefined variables without warning#1
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']

posted date: 2008-12-17 17:26:00


Re: PHP: printing undefined variables without warning#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-17 17:26:01


Re: PHP: printing undefined variables without warning#3
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.

posted date: 2008-12-17 17:31:00


Re: PHP: printing undefined variables without warning#4
echo @$variable;

posted date: 2008-12-17 17:34:00


Re: PHP: printing undefined variables without warning#5
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.

posted date: 2008-12-17 17:35:00


Re: PHP: printing undefined variables without warning#6
Yeah thanks for that. I almost never use this operator for the reasons I mentioned :)

posted date: 2008-12-17 17:39:00


Re: PHP: printing undefined variables without warning#7
the @ operator is slow

posted date: 2008-12-18 06:17:00


Re: PHP: printing undefined variables without warning#8
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.

posted date: 2008-12-18 06:31:00


Re: PHP: printing undefined variables without warning#9
Another reason not to use it

posted date: 2008-12-18 10:36:00


Re: PHP: printing undefined variables without warning#10
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...

posted date: 2008-12-18 16:16:00


Re: PHP: printing undefined variables without warning#11
Fully agree with the suggestion that this is not best practice.

posted date: 2008-12-18 16:42:00


Re: PHP: printing undefined variables without warning#12
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.

posted date: 2008-12-18 16:52:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.