Is this really by design?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 881 Views - Last Post: 30 April 2017 - 06:45 PM

#1 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3700
  • View blog
  • Posts: 13,371
  • Joined: 08-August 08

Is this really by design?

Posted 26 April 2017 - 05:58 PM

I found this bug in php the other day, and someone told me that the official response is that it's not a bug, but by design:
$assoc = ['testing'=>'one', 'test2'=>'two', 3=>'three'];
if(in_array(0, $assoc)) {
    echo "This is insane.<br>";
}

I see no reason it should return true in this example, but it does!
Is This A Good Question/Topic? 0
  • +

Replies To: Is this really by design?

#2 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Is this really by design?

Posted 26 April 2017 - 06:40 PM

Yeah, this is totally broken - known bug, unacknowledged by the language maintainers. (but documented by a user on the php.net manual page for this function) One of the many reasons I say you should simply never use PHP except as a source of "worst-practice" design pitfalls to avoid.

Quote

the official response is that it's not a bug, but by design:


This is clearly nonsense. "Result of bad design choices" != "by design".

I suspect that this bug stems from a collision of the brain-dead array design (associative arrays and arrays are the same thing, so $arr = ['foo', 'bar', 'baz'=>'quux', 0=>"wibble"]; compiles - enough said!) and the idiotic random coercion bugs feature behavior. But there's a simple workaround: stop using PHP.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3700
  • View blog
  • Posts: 13,371
  • Joined: 08-August 08

Re: Is this really by design?

Posted 26 April 2017 - 07:26 PM

Yeah, I know your opinion about php, but asking people to stop using it is like asking them to stop using Windows — which has many more problems than php.

My problem with this issue is that all they need to do to fix it would be to default to strict mode:
$assoc = ['testing'=>'one', 'test2'=>'two', 3=>'three'];
if(in_array(0, $assoc, true)) {
    echo "This is insane.<br>";
} else {
    echo "This worked.<br>";
}


or at least point out the issue prominently in the manual so that people don't have to waste time debugging it.
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Is this really by design?

Posted 26 April 2017 - 07:46 PM

View PostCTphpnwb, on 26 April 2017 - 09:26 PM, said:

or at least point out the issue prominently in the manual so that people don't have to waste time debugging it.


As you pointed out, this would require them to admit that it's a problem. They're not going to do this, because it's a result of fundamental design flaws in the language, and trying to fix those would break all existing PHP installations even more than they're already broken. The only way for PHP's maintainers to proceed is to just pretend that it isn't a problem - which adds one more problem to the pile.

Quote

Yeah, I know your opinion about php, but asking people to stop using it is like asking them to stop using Windows — which has many more problems than php.


Frankly, this sounds like Stockholm syndrome talking. It's actually very easy to stop using PHP - just pick something else and use that.

Anyway, to answer the question: no, it's not a feature anyone ever asked for or designed into the language. It was never meant to work this way, which is obvious from simply looking at the behavior. On the other hand, yes, it's the result of underlying design choices which have created this and many many bugs in PHP.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4128
  • View blog
  • Posts: 13,020
  • Joined: 08-June 10

Re: Is this really by design?

Posted 27 April 2017 - 07:58 AM

View PostCTphpnwb, on 27 April 2017 - 04:26 AM, said:

My problem with this issue is that all they need to do to fix it would be to default to strict mode:


which would break every script that does not convert numbers from external resources:
- in_array(7, $_POST['numbers'])
- in_array(2, $result['id']), when using PDO::FETCH_GROUP and PDO::ATTR_EMULATE_PREPARES / PDO::ATTR_STRINGIFY_FETCHES (or other database results with adequate conversion behaviour)
Was This Post Helpful? 1
  • +
  • -

#6 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6544
  • View blog
  • Posts: 30,650
  • Joined: 10-May 07

Re: Is this really by design?

Posted 27 April 2017 - 10:31 AM

Given your example, would it not be expected that the variable array $assoc contain elements 0, 1, & 3?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3700
  • View blog
  • Posts: 13,371
  • Joined: 08-August 08

Re: Is this really by design?

Posted 27 April 2017 - 01:42 PM

View PostDormilich, on 27 April 2017 - 08:58 AM, said:

View PostCTphpnwb, on 27 April 2017 - 04:26 AM, said:

My problem with this issue is that all they need to do to fix it would be to default to strict mode:


which would break every script that does not convert numbers from external resources:

Does that justify making the problem worse?

This code:
$arr = [1 => '1', 2 => 10, 'test' => '8'];
$x = [1, '1', 2, '10', 10, '8', 'test'];
foreach ($x as $y)
    if (in_array($y, $arr, true)) echo "Found: " . $y . "<br>"; else echo "Did not find " . $y . "<br>";

produces this output:
Did not find 1
Found: 1
Did not find 2
Did not find 10
Found: 10
Found: 8
Did not find test

Shouldn't it?
Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Is this really by design?

Posted 27 April 2017 - 06:51 PM

View Postjon.kiparsky, on 27 April 2017 - 02:46 AM, said:

Frankly, this sounds like Stockholm syndrome talking. It's actually very easy to stop using PHP - just pick something else and use that.

If only the real world were that simple. The choice of language is usually either a team effort, the client's choice, or forced upon you by whatever codebase you are taking over. It's a rare job where nobody but you has an opinion on which language is to be used. - "Picking something else" might literally mean passing on projects or resigning.
Was This Post Helpful? 2
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3700
  • View blog
  • Posts: 13,371
  • Joined: 08-August 08

Re: Is this really by design?

Posted 27 April 2017 - 07:10 PM

View PostAtli, on 27 April 2017 - 07:51 PM, said:

"Picking something else" might literally mean passing on projects or resigning.

True, and if you want to write something that others can use on their web sites, ignoring the language used on something like 75% of websites is going to limit your market potential.

I don't think this is something that indicates a major problem with php either. I think it's a minor problem that the powers that be need to address, just as they need to do with other languages on occasion. Even if they can't fix it quickly they could do better just by letting people know they should use strict mode.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Is this really by design?

Posted 27 April 2017 - 08:27 PM

View PostAtli, on 27 April 2017 - 08:51 PM, said:

"Picking something else" might literally mean passing on projects or resigning.


Yes, that's a big part of what I meant. We programmers are a scarce commodity, and we can pick our work. This is a gift, and we should make use of it.
In today's market, anyone with multiple years of web dev experience can find work, and they do not have to limit themselves to languages they've used before. If you understand the request-response cycle, and you have a good grasp of databases and know how to scratch them behind the ears, you will be able to get work in RoR or Django inside of a few months. There's no reason at all to stay in a relationship with a language that abuses you.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Is this really by design?

Posted 27 April 2017 - 08:42 PM

View PostCTphpnwb, on 27 April 2017 - 09:10 PM, said:

I don't think this is something that indicates a major problem with php either. I think it's a minor problem that the powers that be need to address, just as they need to do with other languages on occasion.


Well, that's the problem. They know it's an issue, and they've known for at least five years (the comment reporting this bug is five years old). I don't know about you, but in my world, when someone leaves a bug open for five years, I do not expect them to fix it. Particularly when they're denying that it's a bug.

Does it indicate a major problem with PHP? Well, you tell me. The language that you depend on to do your work has a basic function which returns ludicrously wrong results which clearly can lead to major bugs and in fact should not be used because it is totally broken*. This bug has been known for five years, cannot be fixed, and the language maintainers deny that it's a bug.

How many major problems with PHP can you count in those two sentences?

* in most language communities, a function which so blatantly violates its contract with the programmer would be called "totally broken" and considered to be "not safe to use". I don't know if this is the convention in the PHP ecosystem.
Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1789
  • View blog
  • Posts: 5,702
  • Joined: 15-January 14

Re: Is this really by design?

Posted 28 April 2017 - 10:55 AM

Quote

It's a rare job where nobody but you has an opinion on which language is to be used.

True dat. That was my situation several years ago when I had the job of rewriting our major application from scratch. It was at the time written in ASP classic, and I made the decision to move it to PHP. I don't regret that decision, there are several design decisions that I didn't get right when designing the actual application, but I don't consider the language to be one of those. Out of the millions of lines of code of Javascript and PHP that have been written for this, 99.9% of the bugs have to do with either the design of the feature or the implementation of it, rather than the fact that it's Javascript or PHP being used.
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3700
  • View blog
  • Posts: 13,371
  • Joined: 08-August 08

Re: Is this really by design?

Posted 28 April 2017 - 11:12 AM

View Postjon.kiparsky, on 27 April 2017 - 09:42 PM, said:

How many major problems with PHP can you count in those two sentences?

None. There's a difference between the baby and the bathwater. Having stumbled on this I now know to use strict mode in the future, so the language will do what I need it to do. If I were to abandon every language where the maintainers have done something I don't like, I wouldn't be able to write anything.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Is this really by design?

Posted 28 April 2017 - 12:19 PM

Quote

The language that you depend on to do your work has a basic function which returns ludicrously wrong results which clearly can lead to major bugs and in fact should not be used because it is totally broken*. This bug has been known for five years, cannot be fixed, and the language maintainers deny that it's a bug.


Are you sure? I get four.
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3700
  • View blog
  • Posts: 13,371
  • Joined: 08-August 08

Re: Is this really by design?

Posted 28 April 2017 - 12:32 PM

Yes, I'm sure. What's broken is the default setting (and the lack of acknowledgement/warning in the docs), but if you use strict mode it will function as it should. If you can show a case where it doesn't, then I'd agree that the function is totally broken.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2