Hacker News new | ask | show | jobs
by ars 4110 days ago
You get json from the browser?

Of course I manipulate the data - that's exactly what weak mode does, convert the strings into integers. I just don't see how doing the conversion myself manually helps anything.

> It throws an error in case you receive bad input, and as we all know you will receive bad input.

It does no such thing. (int) will simply turn bad input into a zero.

1 comments

> You get json from the browser?

Yes.

Really? All the pages you program with forms, and links and whatever are sending you json?

I have no doubt you CAN do it, but most of the time you don't.

And since most of the time you are dealing with strings my question stands: Why do the conversion manually instead of letting the nice new feature do it for you.

You've heard of JavaScript I presume?
Even AJAX sites do not typically send all data back as JSON. Most of the time they use normal form-urlencoded data.

If you did send everything as JSON you would be bypassing everything PHP does with form/url data to make things easier for you (for example arrays). That doesn't seem like a good engineering tradeoff.

Sending structured data from JavaScript to PHP is much easier via json and in PHP its as easy as calling json_decode() to get back and object or array depending on your preference.
PHP accepts nested key value pairs and frameworks can take advantage of that when accessing the $_REQUEST object. If you're bypassing the functionality that's baked into forms then you're going to have to put it back in at some point or reinvent it yourself.

And then you can't make a request to the server directly unless you format your data as JSON, which is a bit inconvenient, especially if you're debugging a problem.

So if using JSON as a container isn't gaining you some other benefit then it's probably best avoided.