Home >Backend Development >PHP Problem >How much do you know about PHP JSON functions?
The previous article introduced you to "What is a PHP filter? What kinds of filters are there? 》, this article continues to introduce to you what is the PHP JSON() function? This article will have certain reference value!
How to encode and decode JSON objects using PHP language?
Environment configuration JSON extension has been built-in in php5.2.0 and above. json_encode performs JSON encoding on variables, json_decode decodes JSON format strings and converts them into PHP variables, and json_last_error returns the last error that occurred.
What is JSON function:
JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.
About json function:
Series Description
# JSON_ENCODE Perform the variable json coding
JSON_DECODE to decode the character string of json format to convey Returns the last error that occurred for PHP variable
json_last_error # The function returns JSON data if executed successfully, otherwise it returns FALSE.
Syntax:
string json_encode ( $value [, $options = 0 ] )
Parameters:
value: The value to be encoded. This function is only valid for UTF-8 encoded data.
options:Binary mask consisting of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
How to use PHP The array is converted into JSON format data. The code is as follows:
<?php $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>The code execution result is:
How to convert PHP objects It is JSON format data, and the code is as follows:
<?php class Emp { public $name = ""; public $hobbies = ""; public $birthdate = ""; } $e = new Emp(); $e->name = "sachin"; $e->hobbies = "sports"; $e->birthdate = date('m/d/Y h:i:s a', "8/5/1974 12:20:03 p"); $e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 12:20:03")); echo json_encode($e); ?>
The code execution result is:
Recommended learning: "
PHP Video Tutorial》
The above is the detailed content of How much do you know about PHP JSON functions?. For more information, please follow other related articles on the PHP Chinese website!