PHP Technology & Digital Applications 2 — Questions and Answers
Question 1: Which PHP function retrieves the current URL of the page being accessed?
- $_SERVER['PHP_SELF']
- $_SERVER['REQUEST_URI'] (Correct answer)
- $_SERVER['HTTP_HOST']
- $_SERVER['DOCUMENT_ROOT']
Correct answer: $_SERVER['REQUEST_URI']
$_SERVER['REQUEST_URI'] returns the URI including the path and query string of the current request.
Question 2: What PHP extension is commonly used to interact with Redis for caching?
- php-redis (Correct answer)
- php-memcache
- php-cache
- php-store
Correct answer: php-redis
The php-redis extension provides a PHP interface to interact with a Redis server for caching and data storage.
Question 3: Which PHP function encodes data to be safely transmitted in a URL?
- htmlspecialchars()
- base64_encode()
- urlencode() (Correct answer)
- addslashes()
Correct answer: urlencode()
urlencode() converts special characters into their percent-encoded equivalents for safe inclusion in URLs.
Question 4: In PHP, which method of a PDO object executes a prepared statement?
- run()
- query()
- exec()
- execute() (Correct answer)
Correct answer: execute()
The execute() method on a PDOStatement object runs the prepared statement, optionally binding parameters.
Question 5: Which PHP built-in server-side variable contains the user's IP address?
- $_SERVER['USER_IP']
- $_SERVER['CLIENT_IP']
- $_SERVER['REMOTE_ADDR'] (Correct answer)
- $_SERVER['HTTP_X_FORWARDED_FOR']
Correct answer: $_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_ADDR'] contains the IP address from which the user is viewing the current page.
Question 6: What does the PHP function `json_decode()` return when passed invalid JSON?
- false
- null (Correct answer)
- 0
- An empty string
Correct answer: null
json_decode() returns null when the input string is not valid JSON.
Question 7: Which PHP function checks if a file exists and is readable?
- file_exists()
- is_readable() (Correct answer)
- is_file()
- file_get_contents()
Correct answer: is_readable()
is_readable() returns true only if the file exists AND the current user has read permission.
Which PHP function retrieves the current URL of the page being accessed?