I was getting the message that unexpected characters were output by my plugin and for the life of me I couldn't work out what I was doing wrong.

Normally this is due to error messages due to coding problems that are not seen by the user since they are hidden by WP as it redirects the page.

Another reason may be space characters outside of the PHP code tags, but I had none of this.

I even tried this code for my plugin:
PHP Code:
<?php ?>
i.e. nothing but the PHP tags, and still I got a warning that 3 unexpected characters were output.

Finally, in desperation I wrote a test script to examine the mysterious characters (this was based on the actual WP code that produces this error):

PHP Code:
'wp-load.php' );

if ( 
ob_get_length() > ) {
$output ob_get_clean();
$len strlen($output);
printf('%x%x%x'ord($output[0]), ord($output[1]), ord($output[2]));

(this script can be saved as say test.php to the root of your blog)

This revealed that there were in fact 3 invisible characters. To cut a long story short, they were caused by me having my code editor set to produce text in UTF-8 encoding format. So there is some preceding code to identify the format causing this problem. Simply saving the plugin code file as ANSI format solved the issue. Also, it seems to work OK if you save in the UTF-8 without BOM encoding format.

So there you go ...