Explore key concepts in handling JSON and XML data using PHP. This quiz covers basic parsing, encoding, decoding, and data structure manipulation techniques essential for PHP developers working with APIs and data exchange formats.
Which PHP function is used to convert a JSON-formatted string into a PHP variable or array?
Explanation: The correct function is 'json_decode', which transforms a JSON string into a PHP variable, usually an array or object. 'json_encode' does the opposite, turning a PHP variable into a JSON string. 'parse_json' and 'decode_json' are not valid PHP functions—these are common errors due to confusing naming conventions.
If you have the PHP array ['red', 'green', 'blue'], which function should you use to convert it into a JSON string?
Explanation: 'json_encode' is the correct function in PHP for converting arrays or objects into JSON-formatted strings. 'array_to_json' and 'encode_json' are not PHP functions, though they may sound logical based on their names. 'array_encode' is also incorrect; PHP does not provide this function out of the box.
What PHP function would you typically use to load and parse an XML string into an object for further access?
Explanation: The 'simplexml_load_string' function allows PHP to convert an XML string into an object for easy data access. 'xml_decode' and 'xml_parse_string' are not standard PHP functions, despite their similar names. 'parse_xml' is also incorrect and may be misleading for beginners.
By default, what type of data structure does 'json_decode' return when applied to an object-based JSON string in PHP?
Explanation: By default, 'json_decode' returns a stdClass object when decoding an object-based JSON string, unless you set the second parameter to true for an associative array. An indexed array is only returned if the JSON is an array. A string would be incorrect, as the returned data is structured, not plain text.
Which function can you use immediately after using 'json_decode' to determine if a decoding error occurred?
Explanation: 'json_last_error' provides the last error code from JSON encoding or decoding operations, making it essential for debugging. The options 'get_json_error', 'json_get_error', and 'last_json_error' are not valid PHP functions and may be confused with typical naming schemes.
When creating XML output in PHP, which function is commonly used to create a new XML document from scratch?
Explanation: Initiating a new XML document in PHP typically involves instantiating a SimpleXMLElement object using 'new SimpleXMLElement'. 'json_encode' handles JSON formatting, not XML. 'loadXMLFile' and 'make_xml_object' are not native PHP functions for creating new XML documents and can mislead beginners.
Which JSON encoding option should be passed to 'json_encode' to produce a human-readable, indented JSON string in PHP?
Explanation: Using the 'JSON_PRETTY_PRINT' option with 'json_encode' ensures formatted and indented JSON output for easier reading. 'JSON_INDENT', 'PRETTY_JSON', and 'JSON_FORMATTED' are not predefined PHP constants, though their names might seem plausible due to their descriptive nature.
What parameter must you set to true in 'json_decode' to receive an associative array instead of an object from a JSON string?
Explanation: Setting the second parameter of 'json_decode' to true returns an associative array. The first parameter is required for the actual JSON string, and there is no global setting for this conversion. Simply calling 'json_decode' without the second parameter will default to returning an object, not an array.
If you load XML data with SimpleXML in PHP, how do you typically access the value of an element called 'title'?
Explanation: With SimpleXML, you access XML elements using object property syntax such as '$xml-u003Etitle'. '$xml['title']' is used for attributes, not element names. '$xml-u003Eget('title')' and '$xml.title' are not valid PHP syntax and will result in errors if used in this context.
Which statement best describes a key difference between handling JSON and XML data in PHP?
Explanation: PHP can directly decode JSON into arrays using 'json_decode' with the appropriate parameter, whereas XML parsing usually yields object-based structures like SimpleXML objects. XML can be made into arrays by additional conversion, but not by default. The other options incorrectly suggest that both formats behave identically or require manual parsing—they do not.