What is the correct syntax to access a JSON object property in PHP that contains a hyphen?
When accessing a JSON object property in PHP that contains a hyphen, you cannot directly use the arrow operator (->) as it is not a valid identifier in PHP. Instead, you can use curly braces and single quotes to access the property. By wrapping the property name containing a hyphen in curly braces and single quotes, you can successfully access the desired property.
// Sample JSON object
$jsonObject = '{"first-name": "John", "last-name": "Doe"}';
// Decode the JSON object
$data = json_decode($jsonObject);
// Accessing property with a hyphen using curly braces and single quotes
$firstName = $data->{'first-name'};
// Output the value
echo $firstName; // Output: John