Learn PHP MCQs

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

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

1) Removes duplicate values from an array

2) Adds unique values to an array

3) Sorts an array uniquely

4) None of the above

Answer : Option 1

How do you include a file only once in PHP?

1) Using `include_once()`

2) Using `require_once()`

3) Using `include()`

4) Using `require()`

Answer : Option 1

Which function is used to remove whitespace from both ends of a string in PHP?

1) trim()

2) rtrim()

3) ltrim()

4) strip()

Answer : Option 1

How do you convert a PHP array into a JSON string?

1) Using `json_encode()`

2) Using `array_to_json()`

3) Using `encode_json()`

4) Using `convert_to_json()`

Answer : Option 1

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

1) Reads the contents of a file into a string

2) Gets the file metadata

3) Reads a file line by line

4) None of the above

Answer : Option 1

How do you make a variable static within a function in PHP?

1) Using the `static` keyword

2) Using the `const` keyword

3) Using the `global` keyword

4) Using the `persistent` keyword

Answer : Option 1

Which function is used to get a random integer within a range in PHP?

1) rand()

2) random_int()

3) get_random()

4) integer_random()

Answer : Option 1

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

1) Filters a variable with a specified filter

2) Validates a variable type

3) Sanitizes a variable

4) None of the above

Answer : Option 1

How do you get the HTTP request method in PHP?

1) Using `$_SERVER["REQUEST_METHOD"]`

2) Using `get_request_method()`

3) Using `request_method()`

4) Using `$_REQUEST["METHOD"]`

Answer : Option 1

What is the output of `echo (int) "0123";` in PHP?

1) 123

2) 0123

3) Error

4) 0

Answer : Option 1