PHP PHP 3 — Questions and Answers
Question 1: Which PHP keyword is used to refer to the current object inside a class method?
- self
- parent
- $this (Correct answer)
- object
Correct answer: $this
$this is the pseudo-variable used inside a class method to refer to the calling object instance.
Question 2: What is the purpose of PHP's PDO extension?
- To create PDF documents
- To provide a database-agnostic interface for data access (Correct answer)
- To parse XML documents
- To handle HTTP requests
Correct answer: To provide a database-agnostic interface for data access
PDO (PHP Data Objects) provides a consistent interface for accessing multiple database types using the same API.
Question 3: Which PHP function converts a string to all uppercase letters?
- str_upper()
- uppercase()
- strtoupper() (Correct answer)
- toUpper()
Correct answer: strtoupper()
strtoupper() converts all alphabetic characters in a string to uppercase.
Question 4: What does the PHP function array_merge() do when two arrays have the same string key?
- Throws an error
- Keeps the first value
- Overwrites with the second array's value (Correct answer)
- Merges values into a nested array
Correct answer: Overwrites with the second array's value
When string keys conflict during array_merge(), the second array's value overwrites the first; numeric keys are renumbered.
Question 5: Which of the following correctly defines an abstract class in PHP?
- interface MyClass {}
- abstract class MyClass {} (Correct answer)
- virtual class MyClass {}
- class abstract MyClass {}
Correct answer: abstract class MyClass {}
The abstract keyword placed before class declares an abstract class that cannot be instantiated directly.
Question 6: What is the correct PHP syntax to catch multiple exception types in a single catch block?
- catch (ExceptionA, ExceptionB $e)
- catch (ExceptionA | ExceptionB $e) (Correct answer)
- catch (ExceptionA || ExceptionB $e)
- catch (ExceptionA + ExceptionB $e)
Correct answer: catch (ExceptionA | ExceptionB $e)
Since PHP 8.0, multiple exception types can be caught in one block using the pipe | separator.
Question 7: Which PHP ini directive controls the maximum size of file uploads?
- post_max_size
- max_file_size
- upload_max_filesize (Correct answer)
- file_upload_limit
Correct answer: upload_max_filesize
upload_max_filesize sets the maximum size allowed for individual file uploads in PHP.
Which PHP keyword is used to refer to the current object inside a class method?