PHP and JSON


JSON (JavaScript Object Notation) represents data as a light and readable format suitable for storage and data transportation. The format serves as a standard for data transfer between web application layers such as frontend JavaScript and backend PHP systems.

 

php and json

Functions in PHP for JSON

Function Description
json_encode() Converts PHP data → JSON string
json_decode() Converts JSON string → PHP data

1. Encoding PHP Data to JSON

<?php
$data = [
    "name" => "Alice",
    "age" => 25,
    "email" => "alice@example.com"
];

$json = json_encode($data);
echo $json;
?> 

Decoding JSON to PHP

<?php
$json = '{"name":"Alice","age":25,"email":"alice@example.com"}';

$data = json_decode($json, true); // true → associative array
echo $data["name"];  // Output: Alice
?> 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.