Learn PHP MCQs

Prepare PHP MCQs (Multiple choice Questions) for exam and job interviews.

Which function is used to convert a string to an array in PHP?

1) explode()

2) str_split()

3) split()

4) array()

Answer : Option 1

What does the `array_pop()` function do in PHP?

1) Removes the last element of an array

2) Adds an element to the end of an array

3) Removes the first element of an array

4) Adds an element to the beginning of an array

Answer : Option 1

Which of the following is a magic method in PHP?

1) __construct()

2) __new()

3) __call()

4) __invoke()

Answer : Option 1

What does the `json_encode()` function do?

1) Converts a PHP array or object into a JSON string

2) Converts a JSON string into a PHP array or object

3) Encodes a string into JSON format

4) None of the above

Answer : Option 1

Which function is used to find the current script name in PHP?

1) $_SERVER["PHP_SELF"]

2) $_SERVER["SCRIPT_NAME"]

3) $_SERVER["REQUEST_URI"]

4) $_SERVER["SCRIPT_FILENAME"]

Answer : Option 1

What is the purpose of the `error_reporting()` function in PHP?

1) Sets the error reporting level

2) Displays all errors

3) Logs errors to a file

4) Terminates script on error

Answer : Option 1

How do you set a default timezone in PHP?

1) date_default_timezone_set()

2) timezone_set()

3) set_timezone()

4) set_default_timezone()

Answer : Option 1

What is the difference between `include()` and `require()` in PHP?

1) require() causes a fatal error if the file is not found, while include() only emits a warning

2) include() causes a fatal error if the file is not found, while require() only emits a warning

3) Both behave identically

4) None of the above

Answer : Option 1

Which function is used to get the MIME type of a file in PHP?

1) mime_content_type()

2) file_mime_type()

3) get_mime_type()

4) mime_type()

Answer : Option 1

How do you convert an object to an array in PHP?

1) get_object_vars()

2) array_from_object()

3) convert_object()

4) object_to_array()

Answer : Option 1