- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
first? :)
Admin
first
Admin
3rd
Admin
Can I get some comment captioning for the PHP-impaired?
Admin
...i don't get it
Admin
A subject just missed ! in the first if.
Admin
Well, my PHP is rusty, but I would say the first biggie is trying to open a file that has already been determined not to exist.
Admin
I haven't had any coffee yet, but it's first checking if a file exists, and then if the _exists check fails then it tries to read it in a loop.
Admin
Going off my limited PHP knowledge.
If the file exists it does what it was supposed to do with the file. If it doesn't not only does it do it anyway, but it goes into a while loop that will continuously generate errors but never leave the loop.
Man oh man, WTF was this guy thinking...? Oh right, 'File not found eh? Well, open it anyway.'
Admin
This code is just screaming for tri-valued booleans.
Admin
...just looking for that nifty third boolean value.
Admin
1st line checks if file named (string)$file exists. If it exists - 2nd line reads file's data into a string $data. This is done by using funcion file(), which reads all file into an array - every sting in a file - an element in an array. After this reading this line implode()'s (joins) that array into string.
If $file doean't exists the interesting part begins :) It still tries to open the file and read every line of it till it would reach the end of file. But what's the point of this else statemenet, if file doeasn't exists? WTF?
Admin
Ouch. As I look up the purpose of the functions, the two if clauses are virtually identical in what they do. The file() function reads the entire file into an array, and implode() takes all elements of an array and concatenates them together into a single string -- in this case, without any separating character. So basically, no matter if the file exists or not, the program will attempt to open the file and create a single string with the file's contents. The one that will actually work (the implode(file()) version), is the most inefficient way of the who.
Next, even after the file has been determined not exist, as I said before, they try to open the file. After calling fopen(), they do not even bother to check to see if the returned value is null. The following loop is probably okay (if they file actually existed and opened successfully!), but in either case, if the file is very large, you could easily run into the resource errors mentioned. I wonder what they were actually trying to do with the file and what kind of files this code was expecting to open.
Admin
The following excerpt from the PHP manual might clarify things:
Basically, the code checks if a file exists, and when it has ascertained the absence of the file, it tries to open it anyway in a way that the manual specifically warns against. The resulting error messages are happily mailed by cron.
Admin
To be more exact, fopen returns FALSE, a warning is issued, and feof(FALSE) is always FALSE, resulting in an infinite loop.
Admin
Is it just me or the code segments in the RSS feed look screwed up?
Admin
I get that, too
Admin
Ah but for a simple "!"...
This required two simple unit tests to validate. sighs
Admin
It is just you brother Ezekiel.
Admin
I see. So if the file exists, then read it. If the file does not exist, then read it slowly. --Rank
Admin
Admin
Yes, this looks like a single character has been left off. As commented, simple unit testing would have found this problem. In the absence of unit testing, it illustrates the value of adopting certain code-writing practices, such as writing the test for the if-case without negation, because that's moderately hard to find by scanning, or commenting the test so that it's easier to verify.
It's not as good as a unit test, but it does help with maintenance.
Admin
It's not the lack of "!". Both branches of the conditional attempt to do the same thing (i.e. read the file into the string). The WTFs are:
Admin
Actually, the else portion is an error notification. It just checks over and over to make sure it gets the administrator's attention.
Admin
Oh wait - I'm an idiot. It's not a missing !, as others have pointed out.
Admin
You said it, not me.
Admin
hehehe :)
Admin
Admin
file_get_contents only exists in PHP 4.3 and later. That part is not a WTF if the script is older than that.
Admin
$data = $data . fread($fp, 1024);
Should have been:
$data .= fread($fp, 1024)
but even then that's just evil. Looks like the $data buffer doubles in size + 1024 every iteration and STILL it's an infinite loop.
captcha: dreadlocks (groovy!)
Admin
The biggest WTF is that he sent PHP to do a job of a PERL, everyone knows that you shouldn't send a boy for a job of a man.
(Yes I hate PHP, for valid reasons too)
Admin
Even if someone happened to create/copy/upload the file while it was infinite looping, it would continue to infinite loop. That is funny to me.
Admin
The PHP api is truly a frightening minefield of inconsistencies.
I once did a very nice online quiz about the murkier parts of PHP's api. It had multiple choice questions like 'what does is_empty(null) return?'. You could also get statistics about the received answers. The median of correct answers was about 80%. I personally landed 92%, and this was after 5 years of intensive work with the language. Can't seem to locate the quiz now, perhaps someone else knows where it lurks?
Anyway, several years of PHP has made me turn to cargo cult programming to preserve my sanity. I do feel occasional pangs of guilt about it, though.
Admin
Maybe he meant
Admin
Admin
I actually love PHP but this statement is all too true in my experience... I know a PHP programmer who will write all his cron jobs/tasks/whatever in PHP. The jobs are usually WAY more suitable for Perl but he insists on using the command line PHP for it.
The right tool for the right job does not mean that you should use a web programming language for scheduled/cron tasks....
Captcha: cognac.... a little early but if you insist!
Admin
I am reminded of a piece of dialogue from an episode of Dexter's Lab called Ocean Commotion:
Captain: Arrr ... the great big crap-like thing ... I've bin searching all me life fer it ... First Mate: Uh, excuse me captain, no you haven't. Captain: Arr - harpoon it anyway!
Admin
Seems like the project manager from Ability to Bend Space-Time a Must went on to become a php programmer. It's the same logic here: If (no survey results yet) then (give me the survey results anyway)
Admin
Hmmm - when we Americans encounter someone who doesn't speak English, don't we try to speak more slowly, as though that will make the other person understand us?
Apparently, this person felt the same way about his code to read a potentially missing file...
BTW: for all who posted that it's missing a "!" (myself included); it was understood (!) that it was supposed to be something like:
which we can all agree is arguably still as stupid as compared with just reading it in one clump.
Admin
"Englishmen abroad" programming.
i.e. If they don't understand what you said in English the first time, say it again louder and slower. They will understand then. :-)
Admin
If it exists, read it.
If it doesn't exist, open it and then read it.
Sort of like this:
Makes sense, yes?
Admin
It seems like the author assumed that if the file does not exist on the local filesystem, then it must be a URL. And under certain PHP configurations, fopen() can open remote files if you pass it a URL. file_exists() however only checks for local files.
Admin
It looks like the author of the snipped was very committed to the idea of not using file() if file_exists() fails. This is generally a good practice, described in the manual. [S]he just missed the else part...
No I doesn't. It's not $data .= $data . fread($fp, 1024);
Admin
There are plenty of reasons to hate PHP, but in general, there is a fairly fixed number of WTFs and once you know them, it's not so bad. Perl, on the other hand is one giant WTF of arcanity and outright wierdness.
Admin
The real WTF here is that PHP actually reads something from that not existing file.
Admin
Good point.
Then again, URL wrappers work with file(), so he could have just used that anyway. And as of PHP 5, file_exists() does actually check over the net for some protocols.
Admin
Remember folks, the acronym for PHP Object Oriented Programming is "POOP".
Admin
Well, actually "is_empty(null)" prints out Fatal error: Call to undefined function is_empty()
The function you meant is called empty() ;-) I mix them up all the time, too ...
One of the biggest WTFs in PHP itself is the fact that a string consisting of a single character zero (the digit zero!) is considered to be empty.
Admin
Some languages support both the "not" keyword and the "!" operator. It's instances like this that I prefer spell it out.
if( not file_exists(foo) ) {
“Programs must be written for people to read, and only incidentally for machines to execute.” – H. Abelson and G. Sussman (in “The Structure and Interpretation of Computer Programs“)
Admin
Addendum (2007-01-29 11:44):
Good point (bad typo, the keys are just next to each other) A good WTF gives a good set of semi-sane answers to the question "what's actually intended"...