DEV Community

I tried to json_decode a string in scientific notation

Segun Olaiya on February 11, 2024

I spent 6 hours trying to work on a bug ticket assigned to me. It turns out the code has been trying to json_decode a floating-point number in scie...
Collapse
 
kwnaidoo profile image
Kevin Naidoo
is_numeric($num) && is_float((float) $num)
Enter fullscreen mode Exit fullscreen mode

You can do the same for ints and other types. There are various other ways you should optimize this function, here are a couple of suggestions:

  1. json_decode($value ?: '') ~ you can just pass a value, you don't need the "?:".
  2. is_array is not valid because json_decode by default returns an object.

For large JSON objects, use "json_validate" instead - it won't parse the whole JSON but just check that it is valid JSON.