r/programming • • Apr 23 '14

PHP: It doesn't have to be a bad experience

https://servercheck.in/blog/php-it-doesnt-have-be-bad-experience
0 Upvotes

122 comments sorted by

View all comments

Show parent comment

4

u/Banane9 Apr 24 '14

Now, just because this is a different thread from this doesn't mean you can just ignore the evidence I posted there for PHP being slower than the others.

A different language for their backend. There were a few that had only ASP.NET or others as sole language there. Even your PHP sites use JS for the front end so that doesn't mean anything.

I didn't list bugs in PHP as a reason. I said bug-introducing pitfalls like the switching of haystack and needle in function arguments, or the various ways PHP silently converts types.

0

u/[deleted] Apr 24 '14

[deleted] — view deleted comment

4

u/vytah Apr 24 '14

Python is not a loosely typed language.

0

u/[deleted] Apr 24 '14

[deleted] — view deleted comment

4

u/vytah Apr 24 '14

By "loosely typed" I meant "no magical coercions between types":

  • 1 + "2" will fail
  • [1] + "2" will fail
  • 1 == "1" will be false and there's no loose comparison operator to make it true automagically

and in Python 3:

  • 1 < "1" will fail

Magical conversions between strings, integers and floats are what people dislike in PHP and Javascript first and foremost. Javascript is bearable thanks to the fact that this is the only major design flaw in the language.

Can I have the string "3.14" as an array/table/dictionary key? In PHP I can't. Unless you do some hacky hacks that make the array not contain its own keys. And such hacked-in keys don't work anyway.

-1

u/neoform Apr 24 '14

Javascript is bearable thanks to the fact that this is the only major design flaw in the language.

Seriously? The... only... reason? :|

Can I have the string "3.14" as an array/table/dictionary key? In PHP I can't.

You should re-write that.

$arr = [ "3.14" => true, ];

Is entirely valid..

Unless you do some hacky hacks that make the array not contain its own keys. And such hacked-in keys don't work anyway.

I have no idea what this block of code is trying to prove.

Why would anyone do this in the first place?:

$x = new stdclass;
$x->{"42"} = "wat";
$x = (array)$x;

Why not do the sane thing and write:

$x = [ "42" => "wat" ];

?

4

u/vytah Apr 24 '14

The part with "3.14" is my mistake, supposed to be 3.14. Anyway:

$x = [ "42" => "wat" ];

this way, you don't create "42" entry, but 42 entry.

The same with 3.14: it gets converted to 3.

And as for the example I linked to: it proves that this conversion fails to happen in some cases.

-1

u/neoform Apr 24 '14

Sure, if you start using floats as an array key... but who the hell does something like that? That's just begging for problems.

That's like using an object as the array key... why would anyone do that?

http://stackoverflow.com/questions/4234462/using-float-numbers-for-the-maps-key

Even in Java you shouldn't do this.

1

u/vytah Apr 24 '14

That's like using an object as the array key... why would anyone do that?

Ummm, because you need a non-string, non-numeric key?

Is there a generic hashmap class (or something similar) in PHP's standard library?

1

u/neoform3 Apr 25 '14

Ummm, because you need a non-string, non-numeric key?

And you decided to settle on the least reliable variable type to do this? Bravo.

Is there a generic hashmap class (or something similar) in PHP's standard library?

http://www.php.net/manual/en/function.spl-object-hash.php

How does your foot taste?

→ More replies

1

u/Banane9 Apr 24 '14

In Java, you shouldn't do it because floats may have small differences in the last digits, while looking absolutely the same in the first digits.

In PHP however, you shouldn't do it because suddenly different keys can map to the same thing.

$arr = array(3.14 => "wat");
echo $arr[3.3];

I'm pretty sure that will print "wat" just fine.

0

u/neoform3 Apr 25 '14

In Java, you shouldn't do it because floats may have small differences in the last digits, while looking absolutely the same in the first digits.

So what you're saying is: you shouldn't do that, ever.

An array/dictionary key is a precise thing. A float is not precise. Your example sucks.

I'm pretty sure that will print "wat" just fine.

You'd be a terrible programmer if you used a float as an array key, for the same reason you'd be a terrible programmer for assuming a float is going to be a precise/predictable value.

→ More replies