EllisLab text mark
Advanced Search
     
White space at the beginning of output
Posted: 12 January 2010 11:05 AM
Avatar
Joined: 2009-10-23
2 posts

Hello!
This is my first post, but i’ve been reading this forum since I started using Code Igniter, and it’s been really useful, thanks to all who contribute to it!

Well, i’ve been trying to do a RSS feed with Code Igniter using this tutorial by Derek Allard. I realized that something really weird was going on because there was always a white space at the beginning of my xml file generated, so I got errors. I tried everything to figure it out, and almost got crazy, but finally I tried this:

Controller: feed.php

<?php
class Feed extends Controller 
{
    
function Feed()
    
{
        parent
::__construct();
    
}
    
    
function index()
    
{
    
echo "WHATEVER";
    
}
}
?> 

And guess what? The output has a white space at the beggining! Does anyone know how what’s causing that?!?!?

I’m using last Codeigniter version: 1.7.2 and PHP 5

Thanx!!

 Signature 

Planet Earth is Dying!!!!

 
Posted: 12 January 2010 11:48 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2003-02-25
256 posts

Do you have any space after your ?>?  The best practice is to delete ?> at the end of your file as long as you don’t have any more HTML below it.

 
Posted: 12 January 2010 12:24 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-07
250 posts

also ...

<?php
    
function Feed()
    
{
        parent
::__construct();
    
}
?> 

should be

<?php
    
function __construct()
    
{
        parent
::Controller();
    
}
}
?> 
 Signature 

flemmingarnott.co.uk

 
Posted: 12 January 2010 12:45 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-10-23
2 posts

JHackamack you’re soooooooo right!!!! Yeah! I’m so happy now! I deleted the ?>, and, yes, after it there was a white space…

Flemming: i thought, that if your server has PHP5 you should use:

parent::__construct(); 

instead of:

parent::Controller(); 

is that right?

Thank you very much, guys!

 Signature 

Planet Earth is Dying!!!!

 
Posted: 12 January 2010 12:49 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-07
250 posts

ah sorry - I guess you’re right if that works! grin

 Signature 

flemmingarnott.co.uk

 
Posted: 13 July 2010 05:41 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2010-07-07
3 posts

I had a similar problem on my end today and found this thread. It turns out that I had a helper (or a library or a plugin, I don’t think that it matters) and there were two extra lines after the closing ?> in the PHP file. Eliminating those lines solved the problem.

So I replaced this

?>
 
 
(end of file

With this

?>
(end of file

And the newlines went away. It makes sense, too, as this helper code is called before any of the output code. It was also included by default on all my pages and so it was appearing elsewhere.

As a corollary to this if you have any spaces after your closing ?> they will also end up in your output. So for example “?>  ” would not be advisable.

In fact, if we go back to what what was said earlier in this thread, REMOVING the ?> from your files will help solve this problem in the future. All of the CI core files use this technique, too. Why shouldn’t we? smile

Enjoy!
—Joel