Hacker News new | ask | show | jobs
by codygman 4110 days ago
> Because everything from the web is a string.

Do you consider JSON a string? Would you manipulate it as a string or use a JSON parser?

> How does putting (int) before the arguments to function help anything?

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

1 comments

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.

> 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.