note 104784 deleted from function.json-encode by cmb
| From: | cmb@php.net | Date: | Tue, 05 Nov 2019 14:31:33 +0000 |
| Subject: | note 104784 deleted from function.json-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212933@lists.php.net to get a copy of this message | ||
Note Submitter: Rijk van Wel
----
The JSON_NUMERIC_CHECK flag introduced in 5.3.0 comes in very handy when handling numbers
encapsulated in a string (database results and post requests are always encoded as string types, for
example). Sending over variables from a database result directly (as string) would cause the
json_encode() function to quote them, which in turn would make the Javascript store them as strings.
As Javascript isn't loosely typed, some libraries could break on this, when attempting to use
one of those variables as row id in a data store for instance. Using the aforementioned flag can
prevent this from happening.
<?php
$arr = array( 'row_id' => '1', 'name' => 'George' );
// fictional db result
echo json_encode( $arr, JSON_NUMERIC_CHECK ); //
{"row_id":1,"name":"George"}
?>