- 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
Let's see...
It must be Monday...
Admin
The signal that this was a WTF is a file named "everything.inc", because any code named "everything" is:
Admin
What happens when I write "$1,000.00"?
Admin
TRWTF: fixString() doesn't even remove the '$' characters.
Admin
He was clenching your teeth? That sounds uncomfortable.
Admin
Yeah. I got burned by something like that once.
The listings for some computers in a point-of-sale system ended up with prices of $1.
(It was script to convert stuff to CSV format. facepalm)
Admin
That should be fine. PHP doesn't give a crap about types so it will assume 1,000.00 is an int after you strip the $.
I think you're supposed to pass an array('$') to the function.
At any rate definitely putting php code in a .inc file is the best way to store server side code.
Admin
I wish I had an "everything.inc". I'd never have to write new code again!
Admin
The real WTF are the double quotes in $cleanedPrice = str_replace(array('$'), "", $price);
Admin
The real WTF is that the solution Jared wrote is so... specific.
$cleanedPrice = preg_replace("/[^0-9.]/", "", $price);
Admin
HA HA ! TRWTF IS PHP.
Admin
That is why he is in charge and you were aren't -
You would have taken 1 minute.
He took 10.
You get paid for 1 minute.
He gets paid for for 10.
Admin
include "cluestick.inc"
fixBrian(3); // 3 strokes should be enough.
Admin
I'd like to know what other gems can be found in everything.inc.
Admin
Admin
I've seen this kind of WTF coding far too much in my time and it just has to stop. Reimplementing an existing API is one of the worst damn things anyone can do. Unfortunately, the PHP api is inconsistent in both naming conventions and use of underscores so it can be easy to get lost when the language looks like spaghetti and is poorly designed.
Admin
$ php -r "var_dump( (int)'1,000.00' );" int(1)
PHP doesn't understand commas when casting strings to ints.
Admin
In some parts of the world, $1,000 means what you'd type as "$1.000". So there is no way for PHP to really understand commas and dots that easily.
Admin
Wait, are you implying that PHP was actually designed? Because it sure looks like a few random pieces of code got mixed together by chance and then started to evolve
Admin
PHP sucks because:
Admin
Admin
Do people REALLY not understand the difference between ==false and ===false in php? That's the reason an 'A' is prepended at the start. My god...
Admin
Admin
What if 'A' is one of the characters you want to replace?
Admin
It was a one line of code, preceded by an include that included the file itself. It created a singularity and exploded into what we know as PHP.
Admin
That's why you include examples of how to write large values in the instructions (such as "1000.00 instead of 1,000.00"). Then, you take the first comma or dot to be the decimal, strip out every other special character, and present the resulting value to the user for confirmation. Eventually, with the program returning a value different from what they expect, the user will figure it out and stop putting extraneous characters in there.
Of course, that may be putting too much faith in the user's ability to figure things out, but it's better than applying a regex to something that varies by region and just blindly accepting what comes out the other end (especially when we're talking about dollar amounts).
Admin
Admin
Admin
Not really. Single or double quotes would both do just fine here. You'd be hard pressed to notice a discernible performance hit from the empty string being parsed by the zend engine.
If the double quotes were around the dollar sign, THEN you'd have a wtf, but a noticeable one.
Admin
No naming conventions? Thats a snobby nit to pick.
The typing issue is pretty vague too; php does suck for the whole weak/dynamic typing thing, but languages like Python also have dynamic typing (though not weak typing), and it adds a lot to usability over pissy static/strongly typed languages like java.
Finally, php4 is now officially unsupported, so it's not entirely fair to say that "everyone" is using it. I don't use php all that much, and but I've been using 5 for a good while.
Php is fine for what it does. Sure it can produce crap code, but it's not a requirement. I'll agree with you on the namespace thing though; that's a royal p.i.t.a.
Admin
Admin
Thats the reason that real languages like .Net include globalization :)
Admin
You joke, but I've seen it over and over, and not just in php...I've seen this crap in java.
There is a certain type of programmer that goes through life with one big honking file in which he includes every method he has ever written, and he imports that massive chunk of crap code into nearly every program he writes.
The worst one I've ever seen was in VB and it was (not joking) 1.5megs of crappy functions dealing with everything from data abstraction, to layout (hardcoded html included), to character manipulations, and to add insult to injury, it was compiled into a DLL, and worse, he had multiple versions of it that all did different things.
I dealt with webapps this joker wrote that were about 50k of app, and about 3 megs of library.
Admin
just use str_replace('$', '', $input) -- no need for an arry. WTF!
Admin
OK.
So does str_replace take care of
$50 $50,000 $50.5 $50.50 40.15 50.00$
I've had to deal with this kind of thing, in the same data file.
Perhaps Brian wrote "everything" when php was brand new, and all the fancy functions were not in the library.
Admin
Pop quiz, what does this return? fixString("ABBBA", array("B");
The answer indicates just how broken this function is.
Admin
At least outputting it in the correct internationalized format is easy enough. Here's Italian, to two decimal places:
Admin
Oh wait....
Admin
ltrim($price,'$');
Admin
TRWTF is: an order form that lets the user enter the price themselves?
Admin
Jared, you're doing it wrong.
People reading through the codebase should only have one library routine that does each thing.
Maybe fixString() was redundant, but now the company's codebase has 2 redundancies.
Remember, code is written once (sorta) and read many times. Two years from now, when someone reads through the code, and they see some people using fixString(), and others using str_replace(), are they going to know why? They probably will not imagine the petty office politics that people are passive-aggressively fighting through the source repository.
Admin
Admin
Behold! This is the glory of fixString()!! You can pass it an array with "$" AND "."
Admin
An unmatched parentheses syntax error.
Admin
ah, much better.
Admin
A fatal error, specifically E_PARSE.
Admin
You could of course look through the request header for a nationality code and then hope that the user has configured his browser AND knows how to write numbers. It is a bit of a burden to lay on your users, though.
Admin
Admin
Weak typing is great for tiny scripts - you can write them in 20 seconds, hit F5 and get an answer. If you're dealing with programs more than 1000 lines of code, or where maintenance is required, strong typing saves a whole load of time in reading documents, source-code and WTFs.
Thank god for small mercies.It doesn't just allow you to make bad code, it encourages it. If a function doesn't specify the types that the arguments take, then you're just asking for trouble.
Admin
But that's just dynamic typing for you. Lots of things use dynamic typing, PHP, Python, Javascript, Ruby, Lua, XML etc. For a lot of things (eg passing the right types of parameters to a function) static typing does have an advantage in that it will find lots of errors at compile-time, but dynamic typing also works well (hence the reason it is used so much).
So, you can't say 'PHP is crap because it uses dynamic typing'. If that were the case, you've pretty much condemned all languages used for web development...