Hacker News new | past | comments | ask | show | jobs | submit login

WTF? Array functions as something GOOD about PHP? It doesn't have a slicing syntax for crying out loud!



Does it really matter to you whether array slicing is implemented as syntactical sugar as opposed to the array_slice() function? If so, why?

In my view the only problem with PHP array functions is the fact that the naming scheme is an absolute disaster (as it is all over PHP, for historical reasons).


Yes, it DOES. Syntax matters. Small improvements in a few places add up.

For example, let's say you've got a string like 4|1|45|343|22. You want to return a string with the last three numbers, but seperated by a comma and a space.

In PHP:

    $arr = explode('|',$str);
    $last_three = array_slice($arr, -3);
    return implode(", ",$last_three);
In Python:

    return ', '.join(str.split('|')[-3:])
Small conciseness improvements snowball as the codebase gets larger. In fact, being unable to chain functions in PHP is actually my biggest beef with the language. If you could do

    return implode(', ',array_slice(explode('|',$str),-3)); 
that'd be a lot less annoying. Still not as good as the python, but a lot closer.


> In fact, being unable to chain functions in PHP is actually my biggest beef with the language.

That's just a lie or at least a horrible misconception, depending on your intentions. Furthermore, the example line you used to illustrate this entirely made-up inability of PHP does in fact work. You can do

  return implode(', ',array_slice(explode('|',$str),-3)); 
it's valid code and it works just as expected!


It doesn't matter. Rather, it matters to me. Why? Because I presumably actually have to use it.


What's that supposed to mean? That PHP devs don't actually have to use array slicing? That languages without a dedicated syntax for array slicing are unusable? Or that you prefer a syntactical shortcut and everyone who disagrees is an idiot?

I mean I'm with you on the fact that it would be nice to have this in PHP. I just don't get how you can argue this point to show that it's supposedly a bad language. The difference between

  array_slice($array, -3)
and

  $array[-3:]
isn't that important to a lot of people. Interestingly, this criticism never comes up when, say, C# or Java are discussed. Only PHP is treated this way. As I said: sure it would be nice to have this feature, but I wouldn't imply that any language that doesn't have it is unusable. Ruby and Python are fortunate in this regard, but this feature alone wouldn't sway my choice either way. In the end, the number of array slicing operations per line of code isn't usually that big.


It means that although it does not truly effect the languages effectiveness, it does negatively effect those who use it. Syntactical sugar exists because people have to use these languages. There is a reason so many languages of the same niche as PHP include array slicing.

"Or that you prefer a syntactical shortcut and everyone who disagrees is an idiot?"

No, not everyone who disagrees with me is an idiot. Just those people who read the sentence "Rather, it matters to me." and somehow think I mean "everyone who disagrees is an idiot", apparently missing the EMPHASIS ON "ME".

(capped for your benefit.)




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: