PHP Communication & Stakeholder Relations 2 — Questions and Answers
Question 1: Which PHP function sends a raw HTTP header to the client before any output is sent?
- header() (Correct answer)
- setcookie()
- ob_start()
- http_response_code()
Correct answer: header()
header() sends a raw HTTP header and must be called before any output is written to the client.
Question 2: What does PHP's mail() function return when the message is successfully accepted for delivery?
- The message ID string
- true (Correct answer)
- 1
- An SMTP response object
Correct answer: true
mail() returns true if the message was successfully accepted for delivery, or false on failure.
Question 3: Which superglobal in PHP contains data submitted via an HTML form using the POST method?
- $_GET
- $_REQUEST
- $_POST (Correct answer)
- $_FORM
Correct answer: $_POST
$_POST contains key-value pairs of data submitted via HTTP POST, typically from HTML forms.
Question 4: What PHP function is used to encode data as a JSON string for API responses?
- json_encode() (Correct answer)
- serialize()
- json_stringify()
- json_pack()
Correct answer: json_encode()
json_encode() converts a PHP value to a JSON-encoded string suitable for API communication.
Question 5: In PHP, which cURL option sets the URL for an HTTP request?
- CURLOPT_URL (Correct answer)
- CURLOPT_URI
- CURLOPT_ENDPOINT
- CURLOPT_HOST
Correct answer: CURLOPT_URL
CURLOPT_URL is the cURL option that specifies the URL to fetch in a request.
Question 6: Which PHP function decodes a JSON string received from a REST API into a PHP variable?
- json_parse()
- json_decode() (Correct answer)
- json_import()
- json_unpack()
Correct answer: json_decode()
json_decode() converts a JSON-encoded string into a PHP object or associative array.
Question 7: What HTTP status code should a PHP API return to indicate a resource was successfully created?
- 200
- 201 (Correct answer)
- 204
- 202
Correct answer: 201
HTTP 201 Created is the standard response for a successful resource creation in a RESTful API.
Which PHP function sends a raw HTTP header to the client before any output is sent?