The Artima Developer Community
Sponsored Link

Java Buzz Forum
God forbid you have extra space at the end of your PHP file!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
God forbid you have extra space at the end of your PHP file! Posted: Dec 1, 2005 12:39 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: God forbid you have extra space at the end of your PHP file!
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

While working on an Ajax app using PHP on the server, I have had an interesting time seeing the quirks of PHP.

The first item to get used to is being in HTML mode by default, so when you want to write real PHP classes and such in their own files you have to make sure to have the magic:

<?php ... your code ... ?>

Normally this isn't a huge deal and you find out quickly.

Recently though a developer made some changes and suddenly no output was to be seen in their area of code. From looking at the errors it told us that "headers can't do anything since you have already started to write to the browser". It turned out that the developer had put in a new file, and it had an extra bit of space at the end of the file.

A PITA for sure. So, now we have a script to check for this :)

#!/usr/bin/perl
# --------------------------------------------------------------------------
# seekingalpha-checkphp - make sure extra space isn't at the end of PHP files
# --------------------------------------------------------------------------

if (@ARGV < 1) {
  print "checkphp: need file(s) to check\n";
  exit;
}

FILE:
foreach my $filename (@ARGV) {

  my @contents = readFile($filename);
  my $IS_SPACE = 0;

  foreach (reverse @contents) {
    if (/\?\>/ and $IS_SPACE) {
      print "Warning: check PHP file: $filename\n";
      next FILE;
    }

    if (!/^\s*$/) {
      print "PHP file OK: $filename\n";
      next FILE;
    } else {
      $IS_SPACE = 1;
    }
  }
}

# --------------------------------------------------------------------------
# Functions
# --------------------------------------------------------------------------
sub readFile {
   my $filename = shift || die "readFile: need a filename";

   open FILE, $filename or die "readFile: couldn't open $filename\n";
   my @contents = <FILE>;
   close FILE;

   return @contents;
}

Read: God forbid you have extra space at the end of your PHP file!

Topic: Outsource Development of Complete Book Store Website (mini-Ebay) for 75$ Previous Topic   Next Topic Topic: Pairing - quote of the week

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use