note 104784 added to function.json-encode
| From: | RijkvanWel at osu1 dot php dot net | Date: | Thu, 07 Jul 2011 09:18:56 +0000 |
| Subject: | note 104784 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181306@lists.php.net to get a copy of this message | ||
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"}
?>
----
Server IP: 83.137.20.135
Probable Submitter: 82.93.68.91
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/104784
Del: integrated -- https://master.php.net/note/delete/104784/integrated
Del: useless -- https://master.php.net/note/delete/104784/useless
Del: bad code -- https://master.php.net/note/delete/104784/bad+code
Del: spam -- https://master.php.net/note/delete/104784/spam
Del: non-english -- https://master.php.net/note/delete/104784/non-english
Del: in docs -- https://master.php.net/note/delete/104784/in+docs
Del: other reasons-- https://master.php.net/note/delete/104784
Reject -- https://master.php.net/note/reject/104784
Search -- https://master.php.net/manage/user-notes.php