nerdexam
Zend

200-710 · Question #59

QUESTION 62 Which of the following PHP values may NOT be encoded to a JavaScript literal using PHP's ext/json capabilities?

The correct answer is B. function() { alert("Hello, world!"); }. Option B is correct because function() { alert("Hello, world!"); } is a JavaScript function literal - not a PHP value at all. PHP's json_encode() can only serialize PHP data types that have JSON equivalents (strings, numbers, booleans, null, arrays, objects), and JSON itself…

Data Formats & Types

Question

QUESTION 62 Which of the following PHP values may NOT be encoded to a JavaScript literal using PHP's ext/json capabilities?

Options

  • A"Hello, world!"
  • Bfunction() { alert("Hello, world!"); }
  • Carray("Hello, world!")
  • Darray('message' => 'Hello, world!')

How the community answered

(21 responses)
  • A
    5% (1)
  • B
    71% (15)
  • C
    14% (3)
  • D
    10% (2)

Explanation

Option B is correct because function() { alert("Hello, world!"); } is a JavaScript function literal - not a PHP value at all. PHP's json_encode() can only serialize PHP data types that have JSON equivalents (strings, numbers, booleans, null, arrays, objects), and JSON itself has no "function" data type. Even PHP's own anonymous functions (closures) cannot be JSON-encoded; json_encode() returns false when given a resource or callable.

Why the distractors are wrong:

  • A - A PHP string maps directly to a JSON string literal: "Hello, world!"
  • C - A sequential PHP array encodes to a JSON array: ["Hello, world!"]
  • D - An associative PHP array encodes to a JSON object: {"message":"Hello, world!"}

Memory tip: Think of JSON as a data-only format. Functions are behavior, not data - JSON (and therefore json_encode()) simply has no concept of executable code. If you see a function/callable in the answer choices, it cannot be JSON-encoded.

Topics

#json_encode#JSON limitations#PHP functions#JavaScript literals

Community Discussion

No community discussion yet for this question.

Full 200-710 Practice