7

In PHP documentation I found following part:

Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].

I'm aware that while PHP couldn't make variable $a.b for register_globals, but this explanation doesn't make me fully happy. While indeed, register_globals would have to do it (or, even if wouldn't have because of ${'a.b'}), but why it affected global variables like $_GET where technically you had to give string since beginning (so PHP programmers couldn't forgot about ${'a.b'} sort of stuff in case of $_GET).

And why it only affects dots and spaces? While I can understand dots (<input type=image>), why it would affect spaces. And what about hyphens?

Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
  • @PhpMyCoder: Well, what I said it was "what I was thinking". But, I'm not happy with what I know already :). – Konrad Borowski Jun 18 '12 at 13:45
  • 3
    From the FAQ: "You should only ask practical, answerable questions based on actual problems that you face". Questions along the lines of "What were the developers of PHP thinking?!" are better suited (but probably not *well*-suited) to [Programmers](http://programmers.stackexchange.com/) (you could start a stackexchange for "What were PHP's developers thinking?!" questions, there are probably enough of them…). – Quentin Jun 18 '12 at 13:46
  • @GlitchMr Agreed. I'm sure someone will be able to share why this is. PHP is stupid, but it's usually stupid for some old, outdated reason. – Bailey Parker Jun 18 '12 at 13:46
  • Another question about one of PHP's (well documented) odd behaviors.. here come the upvotes. http://stackoverflow.com/questions/283751/php-replaces-spaces-with-underlines – Mike B Jun 18 '12 at 13:48
  • Another question would be why you'd want those characters to exist in a request parameter in the first place. They'd have to be escaped in the url as well as in command line requests to PHP scripts. I'd rather stick to 'normal' identifier characters. – GolezTrol Jun 18 '12 at 13:49
  • 1
    same for `parse_str` btw. If you want the unaltered query string, you'd have to fetch it from `$HTTP_RAW_POST_DATA` or `php://input` – Gordon Jun 18 '12 at 13:49
  • 1
    @Quentin I doubt there's enough disk space to store the answers to all those questions... – GolezTrol Jun 18 '12 at 13:50
  • 2
    @MikeB `well documented` - that's going a bit far, isn't it? I agree that we see a lot of questions here that are a direct result of people failing to RTFM, but there are *many* areas in which the PHP docs are severely lacking when it comes to explaining *why* things are the way they are. – DaveRandom Jun 18 '12 at 13:50
  • @GolezTrol PHP should be able to handle the escapes in both the name and value of the request parameters because you are allowed to include them in the request parameters. – Bailey Parker Jun 18 '12 at 13:51
  • @DaveRandom IMHO API docs don't need to explain the motivations behind functionality, only their effects. Would you want every function doc to include commit messages and links to mail-list discussions or do you just want to know how `parse_str()` works? Look at http://stackoverflow.com/a/283781/46675, that seems like pretty good explanation to me. – Mike B Jun 18 '12 at 13:52
  • @Quentin So an actual problem is not when I have a `` and I spend hours trying to figure out why `$_GET['user.name']` is empty on form submit? – Bailey Parker Jun 18 '12 at 13:53
  • *(reference)* http://lxr.php.net/xref/PHP_5_4/ext/standard/string.c#4209 – Gordon Jun 18 '12 at 13:53
  • 2
    @PhpMyCoder — That was an actual problem, which you solved. This question is an academic question about why the (solved) problem existed in the first place. – Quentin Jun 18 '12 at 13:54
  • @Quentin Hypothetically say I did ask that question. And let's say many SO users were able to answer. If there was an answer that provided a reason *why* the PHP devs did this along with the solution, wouldn't you be more likely to upvote it than the rest? – Bailey Parker Jun 18 '12 at 13:57
  • @MikeB Fair comment I suppose, but there are definitely some places where the docs are just plain silly, casing point: http://php.net/manual/en/function.current.php - Look at the initial function description, also note that it claims the argument is taken by reference when in fact it isn't. – DaveRandom Jun 18 '12 at 13:58
  • 2
    Verging on dupe: http://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays – DaveRandom Jun 18 '12 at 13:58
  • @PhpMyCoder — An answer that (correctly) solves a practical problem and supplies some background information to support is is likely to be a good answer – Quentin Jun 18 '12 at 14:01
  • @Quentin So if you expect a good answer to include background information, why do you frown upon explicitly asking for background information? – Bailey Parker Jun 18 '12 at 14:03
  • @PhpMyCoder — I don't make the rules. – Quentin Jun 18 '12 at 14:06
  • @Quentin `I don't make the rules.` - first I've heard of this. Who's driving then? :-P – DaveRandom Jun 18 '12 at 14:08
  • @DaveRandom — http://stackoverflow.com/about – Quentin Jun 18 '12 at 14:08
  • I hope that after this will be closed, nobody will make "question" on meta about it – Konrad Borowski Jun 18 '12 at 14:09
  • @DaveRandom Absolutely, I didn't mean to imply the PHP docs are perfect.. and there are definitely cases where a eyebrows are raised and I'm left scrolling through the comments looking for an explanation. – Mike B Jun 18 '12 at 14:09
  • @GlitchMr Delete it if your worried about people discussing you on meta ;-) – DaveRandom Jun 18 '12 at 14:10
  • @DaveRandom It's more about that it would be question about question about question. – Konrad Borowski Jun 18 '12 at 14:11
  • @GlitchMr The validity of such a question would definitely be *questionable* (oh, aren't I funny) – DaveRandom Jun 18 '12 at 14:17
  • @PhpMyCoder `PHP should be able to handle`. And PHP does, just not through the $_REQUEST superglobal. You'd have to put in some more work. Is it silly to put a space in an input parameter of any kind? Yes. Is it silly that PHP doesn't support it? Also yes. Probably a choice from the past, that cannot easily be changed, because it would break existing code. Solution: use normal input variable names. :) – GolezTrol Jun 19 '12 at 05:46

0 Answers0