What is the function json_decode() used for in PHP?
The json_decode() function in PHP is used to decode a JSON string and convert it into a PHP variable. This function is commonly used when working with APIs that return data in JSON format, allowing the data to be easily manipulated within a PHP script.
<?php
// JSON string to be decoded
$json_string = '{"name": "John", "age": 30, "city": "New York"}';
// Decode the JSON string into a PHP variable
$decoded_data = json_decode($json_string);
// Access the decoded data
echo $decoded_data->name; // Output: John
echo $decoded_data->age; // Output: 30
echo $decoded_data->city; // Output: New York
?>
Keywords
Related Questions
- How does include() differ from traditional functions in PHP, and what considerations should be taken when using it for file inclusion?
- What are the potential pitfalls of using fixed positions for tables in PHP image generation?
- When handling database connections in PHP, what are some common best practices to avoid errors like "Call to a member function fetch() on a non-object"?