Comment On "PHP has an eval function like perl"

One of the sysadmins at Stuart Caie's company — Jim, as we'll call him — wanted to scratch his itch. Because he spent a whole lot of time logging into different systems just to gather operational data, he figured that a simple web application could automate the data collection process. "This way," he persuadingly said to management, "I can be much more efficient when doing my job." [expand full text]
« PrevPage 1 | Page 2Next »

Re: "PHP has an eval function like perl"

2009-10-26 09:12 • by FragFrog (unregistered)
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 :]

Re: "PHP has an eval function like perl"

2009-10-26 09:14 • by EatenByAGrue (unregistered)
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.

Re: "PHP has an eval function like perl"

2009-10-26 09:20 • by EmperorOfCanada (unregistered)
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.

Re: "PHP has an eval function like perl"

2009-10-26 09:22 • by Nano (unregistered)
288934 in reply to 288931
Fragfrog: eval != variable expansion

Re: "PHP has an eval function like perl"

2009-10-26 09:24 • by ZanderZ (unregistered)
288935 in reply to 288931
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 :)

Re: "PHP has an eval function like perl"

2009-10-26 09:24 • by ZanderZ (unregistered)
288936 in reply to 288931
FragFrog:
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 :]


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 :)

(Damnit, forgot to quote the message in my previous post)

Re: "PHP has an eval function like perl"

2009-10-26 09:34 • by highphilosopher (unregistered)
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.

Re: "PHP has an eval function like perl"

2009-10-26 09:37 • by Jim Lard
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.

Re: "PHP has an eval function like perl"

2009-10-26 09:41 • by noway! (unregistered)
288939 in reply to 288931
FragFrog:
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.

That's variable expansion, not code evaluation. Although, i think the quotes are redundant... right?

Re: "PHP has an eval function like perl"

2009-10-26 09:41 • by Myname! (unregistered)
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 "".

Re: "PHP has an eval function like perl"

2009-10-26 09:43 • by Book title (unregistered)
"Learn PHP in 21 hours"

FTFY.

Re: "PHP has an eval function like perl"

2009-10-26 09:45 • by Myname! (unregistered)
288942 in reply to 288940
Myname!:
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 "".


Actually, scratch that, just realised what the code was actually doing. Where's my coffee...

Re: "PHP has an eval function like perl"

2009-10-26 09:51 • by Schmalls (unregistered)
288943 in reply to 288939
noway!:
FragFrog:
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.

That's variable expansion, not code evaluation. Although, i think the quotes are redundant... right?


No the quotes aren't redundant. It is doing the same thing as:
$x[hostid] . '.' . $x[hostname]
However, a big WTF is that the array key isn't quoted like so:
$x['hostid'] . '.' . $x['hostname']
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.

Re: "PHP has an eval function like perl"

2009-10-26 09:54 • by The_Assimilator (unregistered)
Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.


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

Re: "PHP has an eval function like perl"

2009-10-26 10:07 • by Anon Ymous (unregistered)
288945 in reply to 288944
The_Assimilator:
Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.


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


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?)

Re: "PHP has an eval function like perl"

2009-10-26 10:11 • by Kermos
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...

Re: "PHP has an eval function like perl"

2009-10-26 10:15 • by Robo (unregistered)
288947 in reply to 288945
"transitioning the application the development team"?

Re: "PHP has an eval function like perl"

2009-10-26 10:20 • by Alex (unregistered)
288948 in reply to 288944
Pedantic exception in PHP Parse error: parse error:

Supplied sentence does contain valid grammar, it does however contain invalid grammar as well.

Lol

Re: "PHP has an eval function like perl"

2009-10-26 10:20 • by Jim (unregistered)
Now that I am redundant, I would just like to say that it's no fun being redundant.

Re: "PHP has an eval function like perl"

2009-10-26 10:21 • by MyKey_
288950 in reply to 288946
Kermos:
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...



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/.]

Re: "PHP has an eval function like perl"

2009-10-26 10:28 • by mbvlist
288951 in reply to 288943
Schmalls:


No the quotes aren't redundant. It is doing the same thing as:
$x[hostid] . '.' . $x[hostname]
However, a big WTF is that the array key isn't quoted like so:
$x['hostid'] . '.' . $x['hostname']
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.

It doesn't work like that when in a string AFAIK. You can test it using this code:
$array['true'] = 'The Real WTF is PHP syntax.';
$var1 = "$array[true]";
$var2 = $array['true'];

I think $var1 and $var2 have the same value. Don't ask me why, it is pretty much crap anyway.

Re: "PHP has an eval function like perl"

2009-10-26 10:29 • by sao (unregistered)
288952 in reply to 288950
MyKey_:
Kermos:
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...



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/.]


agreed, but only initialized pointers

Re: "PHP has an eval function like perl"

2009-10-26 10:30 • by Anonymous (unregistered)
"Personal Home Page". That is all.

Re: "PHP has an eval function like perl"

2009-10-26 10:31 • by Severity One
288954 in reply to 288931
FragFrog:
...but to write truely good PHP code is both possible as well as just as hard as writing good code in any other language.

I would say it's a lot harder. :)

Re: "PHP has an eval function like perl"

2009-10-26 10:36 • by anonymous (unregistered)
288956 in reply to 288945
Anon Ymous:
The_Assimilator:
Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.


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


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?)


The problem is that it should be "at transitioning the application TO the development team".

Re: "PHP has an eval function like perl"

2009-10-26 10:36 • by Anonymous (unregistered)
288957 in reply to 288950
MyKey_:
[UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]

XKCD currently has the best Geocities commemoration I've ever seen - complete with blink tags, broken images and malformed HTML aplenty. It looks exactly like any Geocities page of the nineties. Good times.

Re: "PHP has an eval function like perl"

2009-10-26 10:39 • by Lee K-T (unregistered)
FragFrog:

A LOT of crap gets written in PHP because people don't know what they are doing...


<FlameWar>
Well if they knew, they wouldn't be writing in PHP!!! :)
</FlameWar>

Re: "PHP has an eval function like perl"

2009-10-26 10:43 • by Schmalls (unregistered)
288959 in reply to 288951
mbvlist:

It doesn't work like that when in a string AFAIK. You can test it using this code:
$array['true'] = 'The Real WTF is PHP syntax.';
$var1 = "$array[true]";
$var2 = $array['true'];

I think $var1 and $var2 have the same value. Don't ask me why, it is pretty much crap anyway.

You are correct. It doesn't matter since the argument does hold for Jim's code:
$hostid = $aservers[$i][hostid];

$hostname = $aservers[$i][hostname];

Re: "PHP has an eval function like perl"

2009-10-26 10:53 • by Airhead
288960 in reply to 288940
Myname!:
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 "".


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.

Re: "PHP has an eval function like perl"

2009-10-26 10:58 • by Airhead
288961 in reply to 288954
Severity One:
FragFrog:
...but to write truely good PHP code is both possible as well as just as hard as writing good code in any other language.

I would say it's a lot harder. :)

So, you don't know PHP? ;)

Re: "PHP has an eval function like perl"

2009-10-26 11:07 • by Martin (unregistered)
288962 in reply to 288932
EatenByAGrue:
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.


How can you please email a photograph? The photograph needs to be developed and scanned before emailing.

Stick to the process in future!

Re: "PHP has an eval function like perl"

2009-10-26 11:10 • by Beaker (unregistered)
288963 in reply to 288932
EatenByAGrue:
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.


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.

Re: "PHP has an eval function like perl"

2009-10-26 11:23 • by justsomedude (unregistered)
288964 in reply to 288957
Anonymous:
MyKey_:
[UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]

XKCD currently has the best Geocities commemoration I've ever seen - complete with blink tags, broken images and malformed HTML aplenty. It looks exactly like any Geocities page of the nineties. Good times.



94 Errors, 22 warning(s)
Oh Baby! LOL @ <HTML WEB="2.0"> and the GOTO 10 tag.


Re: "PHP has an eval function like perl"

2009-10-26 11:51 • by Anthony (unregistered)
288965 in reply to 288951
mbvlist:
Schmalls:


No the quotes aren't redundant. It is doing the same thing as:
$x[hostid] . '.' . $x[hostname]
However, a big WTF is that the array key isn't quoted like so:
$x['hostid'] . '.' . $x['hostname']
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.

It doesn't work like that when in a string AFAIK. You can test it using this code:
$array['true'] = 'The Real WTF is PHP syntax.';
$var1 = "$array[true]";
$var2 = $array['true'];

I think $var1 and $var2 have the same value. Don't ask me why, it is pretty much crap anyway.


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...

Re: "PHP has an eval function like perl"

2009-10-26 12:16 • by Jay (unregistered)
288967 in reply to 288946
Kermos:
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...



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.

Re: "PHP has an eval function like perl"

2009-10-26 12:23 • by Jay (unregistered)
288968 in reply to 288963
Beaker:
EatenByAGrue:
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.


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.



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.

Re: "PHP has an eval function like perl"

2009-10-26 12:27 • by Anonymous (unregistered)
288969 in reply to 288964
justsomedude:
Anonymous:
MyKey_:
[UPDATE: Sorry, I forgot the mandatory http://xkcd.com/138/.]

XKCD currently has the best Geocities commemoration I've ever seen - complete with blink tags, broken images and malformed HTML aplenty. It looks exactly like any Geocities page of the nineties. Good times.

94 Errors, 22 warning(s)
Oh Baby! LOL @ <HTML WEB="2.0"> and the GOTO 10 tag.

My favourite:

<SCRIPT LANGUAGE="QBASIC">IF $BROWSER = "IE" THEN GOTO 50</SCRIPT>

...also the single <JAVA> tag that appears for absolutely no reason.

Re: "PHP has an eval function like perl"

2009-10-26 12:50 • by BentFranklin
288970 in reply to 288968
Jay:
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.


Unless your aunt was Grace Hopper.

Re: "PHP has an eval function like perl"

2009-10-26 12:53 • by Procedural (unregistered)
288971 in reply to 288949
Jim:
Now that I am redundant, I would just like to say that it's no fun being redundant.


That's completely true.

Re: "PHP has an eval function like perl"

2009-10-26 12:54 • by evilspoons
I'm surprised no one's mentioned the irony of Jim writing the system that was used to replace Jim.

Re: "PHP has an eval function like perl"

2009-10-26 12:56 • by Rocketeer (unregistered)
I enjoyed reading that code - it was like a horror story for programmers :-)


$hostid = $aservers[$i][hostid];
$hostname = $aservers[$i][hostname];
$systemkey = $hostid . "." . $hostname;

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.


for ($j=0; $j < count($akeys); $j++) {
if ($systemkey == $akeys[$j]) {
$ainstall_values[$i] = $ainstall[$systemkey];
}
}

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...


// PHP has an eval function like perl.
// Building up a switch statement to run later.
$switch = "switch (\$ainstall[\$systemkey]) {";
$avalues[count($aunique)];

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!

Re: "PHP has an eval function like perl"

2009-10-26 13:12 • by nube (unregistered)
Why is this sort of thing a web application ? A system type person should know better.

Re: "PHP has an eval function like perl"

2009-10-26 13:33 • by EngleBart (unregistered)
288976 in reply to 288937
highphilosopher:
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.
I think the problem is that he learned PHP and how to program in 21 days!

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)

Re: "PHP has an eval function like perl"

2009-10-26 13:35 • by ZP (unregistered)
288977 in reply to 288945
Anon Ymous:
The_Assimilator:
Jim aggressively fought off all attempts at transitioning the application the development team so, during a round of cost cutting, Jim was made redundant.


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


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?)


You completely missed the whole.

Re: "PHP has an eval function like perl"

2009-10-26 13:47 • by Dan (unregistered)
Since it would have been wasteful to rewrite it from scratch, there are still vestiges of Jim in there


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.

Re: "PHP has an eval function like perl"

2009-10-26 13:56 • by dtaylor84
288981 in reply to 288931
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.

Re: "PHP has an eval function like perl"

2009-10-26 14:08 • by Procedural (unregistered)
288982 in reply to 288976
EngleBart:
highphilosopher:
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.
I think the problem is that he learned PHP and how to program in 21 days!

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)


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.

Re: "PHP has an eval function like perl"

2009-10-26 14:19 • by Ralph (unregistered)
288983 in reply to 288965
Anthony:


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...


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.

Re: "PHP has an eval function like perl"

2009-10-26 14:42 • by JRiddy
288984 in reply to 288970
BentFranklin:

Unless your aunt was Grace Hopper.


Nice reference.

Re: "PHP has an eval function like perl"

2009-10-26 15:04 • by Shishire (unregistered)
288986 in reply to 288951

<?php
define('foo', 'bar');

$var[foo] = 'baz';
$var['foo'] = 'random';

echo "\$var[foo] = $var[foo]\n";
echo "\$var[bar] = $var[bar]\n";
echo '$var[\'foo\'] = '.$var['foo']."\n";
echo '$var[foo] = '.$var[foo]."\n";
echo '$var[\'bar\'] = '.$var['bar']."\n";

?>

Output:

$var[foo] = random
$var[bar] = baz
$var['foo'] = random
$var[foo] = baz
$var['bar'] = baz


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?
« PrevPage 1 | Page 2Next »

Add Comment