JSON In PHP
A common use of JSON is to read data from a web server, and display the data in a web page.
Here how to exchange JSON data between the client and a PHP server.
The PHP File
PHP has some built-in functions to handle JSON.
Objects in PHP can be converted into JSON by using the PHP function json_encode():
Sample
// Creating an object
$myObj= new myObj("John", 30,"New York");
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj);
echo $myJSON;
json_decode();
$In PHP, the json_decode() function is used to decode a JSON string and convert it into a PHP variable or object. It takes a JSON string as input and returns the corresponding PHP representation.
jsonString = '{"name":"John","age":30,"city":"New York"}';
// Decoding JSON string
$data = json_decode($jsonString);
// Accessing decoded data
echo $data->name; // Output: John
echo $data->age; // Output: 30
echo $data->city; // Output: New York