- 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
And it keeps saddening me to see things like this:
$type = $ainstall["$x[hostid].$x[hostname]"];
Guess what you're doing? That's quite right: in your bright, shiny example you are letting PHP eval that string, the very same principle you're bashing the original writer for.
A LOT of crap gets written in PHP because people don't know what they are doing, but to write truely good PHP code is both possible as well as just as hard as writing good code in any other language. It's just that PHP makes writing bad code so much easier :]
Admin
The real question is whether Jim's reporting application generated paper reports that could be faxed to the head office, placed on a wooden table, photographed, and the photograph emailed out.
Admin
Maybe he had tonnes of Lisp/AI experience and was planning on building a self coding dynamic intelligent system? Or maybe he was a dumbass.
Admin
Fragfrog: eval != variable expansion
Admin
QFT. Code like this gives me physical pain. Luckily I don't have a job as programmer so I don't have to maintain other peoples code :)
Admin
(Damnit, forgot to quote the message in my previous post)
Admin
Unfortunately it's people like this that give PHP a bad name. There's nothing wrong with PHP, just something wrong with learning any language in 21 days.
Admin
Why does Jim need both "hostid" and "hostname" in his key anyway? Are there multiple servers with the same "hostid" but different "hostname"? I guess that's possible - if a machine is being dual-booted for multiple purposes for example. But are there also multiple servers with the same "hostname" but different "hostid"? That seems rather less likely.
Admin
Admin
The suggested code is a WTF in itself. If the value doesn't exist, then there's no need to set the result to "null" because it's already the type NULL which would turn into "null" at any string conversion. If the value was an empty string as the bad code suggested then isset() would return true and it'll stay as "".
Admin
"Learn PHP in 21 hours"
FTFY.
Admin
Actually, scratch that, just realised what the code was actually doing. Where's my coffee...
Admin
No the quotes aren't redundant. It is doing the same thing as:
However, a big WTF is that the array key isn't quoted like so: When left unquoted, PHP first checks to see if there are constants named hostid and hostname and if there are none it emits a notice and uses the string instead.Admin
PHP Parse error: parse error: supplied sentence does not contain valid grammar in C:\Inetpub\wwwroot\tdwtf\Articles\PHP-has-an-eval-function-like-perl.aspx on line 5
Admin
There's nothing wrong with the grammar of that sentence. "Fight off" is a phrasal verb -- it has a different meaning than "fight". (Or are you whining about the use of the passive voice?)
Admin
Dunno if I can really say WTF here. The story itself clearly states this guy had absolutely no programming background and apparently, when he first wrote this application, I it doesn't appear there were any developers available to hand the task off to.
Given that the developer team later on didn't completely scrap the entire code base and was able to refactor it to me indicates that overall, it couldn't have been that bad. I think primarily the project just grew to a size where this guy could no longer handle it with his limited experience.
I think we all can agree that every one of us has produced WTF Worthy code when we first started writing software.
Had this guy been someone claiming to be the greatest programmer of all time with a 6 digit salary, then yes, I'd definitely say WTF. But seeing how the story clearly states he was a beginner, I'd say rather than pointing at him and screaming WTF, someone should maybe consider giving a person like that some pointers in how to do things better. Never know, might actually get a great developer out of it eventually...
Admin
"transitioning the application the development team"?
Admin
Pedantic exception in PHP Parse error: parse error:
Supplied sentence does contain valid grammar, it does however contain invalid grammar as well.
Lol
Admin
Now that I am redundant, I would just like to say that it's no fun being redundant.
Admin
Agreed! But giving pointers to a newbie is a very, very bad idea. Hints are better!
[UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]
Admin
I think $var1 and $var2 have the same value. Don't ask me why, it is pretty much crap anyway.
Admin
agreed, but only initialized pointers
Admin
"Personal Home Page". That is all.
Admin
Admin
The problem is that it should be "at transitioning the application TO the development team".
Admin
Admin
Admin
Admin
Setting variables is A Good Thing. If you need to check for that variable later, you don't allways have to check it with
<?php if (isset($variable) && $variable != null) .... ?>Some of us use debugging with php.
Admin
Admin
How can you please email a photograph? The photograph needs to be developed and scanned before emailing.
Stick to the process in future!
Admin
Sadly, that feature is not implemented yet. There are links on the page, and the managers are afraid that they might accidentally click on them. That would be very confusing for the user, so they need to find a way to disable any links before they can fax reports.
Admin
Admin
Correct, because within quotes it will never evaluate it to a constant (unless using the constant() function).
What Schmalls was getting at is this
$array['true'] = 'The Real WTF is PHP syntax.'; $var1 = "$array[true]"; $var2 = $array['true']; $var3 = $array[true];
$var1 === $var2 != $var3
Now, there is ambiguity in the quoted variable syntax. That's why I try to avoid it completely. But if I do it, I'll use the curly brace syntax to force the variable. So instead of "$array[true]", it would be "{$array['true']}".. That allows you to also do things like "{$word}[something]" without it trying to parse it as an array...
Admin
If an self-trained person on his first attempt at programming writes code that is so bad that it's funny, it is unfair to ridicule him. How do you expect him to learn until he gains some experience?
But the code is still funny.
And just because it's unfair to ridicule someone doesn't mean that we don't enjoy doing it.
Admin
I wouldn't be surprised if in a couple of decades we don't have paper thin computers that you CAN handle like a sheet of paper and poke your finger on a link to bring up the referenced page. And then people will read old jokes about links on print-outs and scratch their heads saying, "I don't get it. So the print-out had links in the text. What's the joke?"
I saw a comedy that was made in the 60s where a guy is trying to bluff his way through a job interview. He's asked if he has any experience working with computers, and he says yes. Then he's asked where he gained this experience, and he says "My aunt had a computer." In the 60s this must have seemed absurd, like claiming that your aunt had a nuclear reactor in her tea room. But of course today, I'm sure many people have aunts who have computers at home.
Admin
Admin
Unless your aunt was Grace Hopper.
Admin
That's completely true.
Admin
I'm surprised no one's mentioned the irony of Jim writing the system that was used to replace Jim.
Admin
I enjoyed reading that code - it was like a horror story for programmers :-)
It started off with a little light foreboding - just as we all know "It was a dark and stormy night..." is going to lead to trouble, so does gratuitous variable creation.
Then, as the story progresses, we start to feel fear at the unnecessary inner loop... okay, so far, it's a touch amateurish, but that's to be expected - yes, there's an easier way to do all this, but it's not actually bad per-se...
Then, like a scream cutting through the night... Eval? Nominative determinism raises it's head - just as Jack the Ripper is unlikely to be planning to convert your CDs to MP3s, a function named Eval is unlikely to be planning on anything good...
To be honest, at this point I went and hid behind the sofa... I heard some more screaming, and a mechanical voice declaiming "E-val-u-ate... E-val-u-ate..." but I didn't dare look at the screen again until the end credits started rolling!
Admin
Why is this sort of thing a web application ? A system type person should know better.
Admin
A programmer with a solid foundation in a single language should be able to produce workable code (not necessarily pretty) in a new language. (If there is a major paradigm shift, e.g. procedural to functional or procedural to tree(XSLT), add a few months for the brain to re-plasticize)
Admin
You completely missed the whole.
Admin
In my experience, it's sometimes better to learn from the first attempt, then scrap it entirely and rewrite it. You often come up with better organization and improve maintainability to the nth degree that way. I've been involved in too many projects where "we don't have time to really fix it, so just fix the problem" and the "just fix it" only made the code harder to work with and often introduced even more problems.
Admin
Actually, no. In that example PHP is interpolating the string, not evaluating the string. Both can be problematic, at times, but interpolating input is a whole lot less likely to get you in trouble than evaluating input.
Admin
I hear they may possibly hypothetically be writing a follow-up to "Learn PHP in 21 Days". It's be called "Unlearn Your Hack Habits in 2.5 Years".
It may not sell as much, but the market is huge.
Admin
Using true here is an array key is a bad example, because its also reserved for boolean truth, but the PHP wtf is that:
$array['key'] = 'value';
$var1 = $array['key']; $var2 = $array[key]
Now $var1 === $var2, but you will get a warning on the line assigning $var2 about using constants as an array key, but the interpreter will guess your intention and use the string if it finds it. === is an operator that checks both type and value, so:
1 == "1" //true 1 === "1" //false
More WTF,
$int = 1; $string1 = "the value of int is $int"; $string2 = 'the value of int is $int';
$string1 will contain "the value of int is 1", while $string2 is "the value of int is $int" , single quotes are dumb strings, but when double quotes are used the interpreter will look at your string and insert any variables it finds.
Admin
Nice reference.
Admin
The point here being that single and double quotes do different things, constants are resolved first, and that array keys inside of single quotes don't need to be re-quoted.
That being said, imho, it's never a good idea to mix variables into static strings, which is why I always use single quotes, and break out whenever I need to insert a variable. Much safer, and no messy escape syntax to deal with (with the exception of ' and \ ).
Captcha: odio Odious much?