Home  >  Article  >  Backend Development  >  How much do you know about PHP JSON functions?

How much do you know about PHP JSON functions?

慕斯
慕斯Original
2021-05-28 18:02:452230browse

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 much do you know about PHP JSON functions?

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(&#39;a&#39; => 1, &#39;b&#39; => 2, &#39;c&#39; => 3, &#39;d&#39; => 4, &#39;e&#39; => 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(&#39;m/d/Y h:i:s a&#39;, "8/5/1974 12:20:03 p");
   $e->birthdate = date(&#39;m/d/Y h:i:s a&#39;, strtotime("8/5/1974 12:20:03"));
   echo json_encode($e);
?>

The code execution result is: How much do you know about PHP JSON functions?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn