Learn PHP MCQs

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

How can you ensure that a PHP script only runs in a command-line interface (CLI)?

1) if (PHP_SAPI == "cli") {}

2) if (is_cli()) {}

3) if (php_sapi() == "cli") {}

4) if (PHP_CLI) {}

Answer : Option 1

What is the default value of the `error_reporting` directive in PHP?

1) E_ALL

2) E_NOTICE

3) E_WARNING

4) E_ERROR

Answer : Option 1

How do you declare an abstract method in a PHP class?

1) abstract function methodName();

2) function methodName();

3) abstract methodName();

4) function abstract methodName();

Answer : Option 1

Which function is used to sort an array by its keys in descending order in PHP?

1) krsort()

2) ksort()

3) asort()

4) arsort()

Answer : Option 1

How do you create a constant in PHP that is accessible within the class only?

1) const NAME = "value";

2) define("NAME", "value");

3) private const NAME = "value";

4) static const NAME = "value";

Answer : Option 3

Which function is used to count all the elements in an array, including nested arrays?

1) count()

2) sizeof()

3) array_count()

4) count_recursive()

Answer : Option 1

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

1) It converts an array into a list

2) It splits a string into an array

3) It assigns values from an array to variables

4) It lists all variables

Answer : Option 3

Which operator is used for string concatenation in PHP?

1) &

2) .

3) +

4) ++

Answer : Option 2

How do you remove all session variables in PHP?

1) session_unset()

2) session_destroy()

3) unset()

4) destroy_session()

Answer : Option 1

How can you convert an array to JSON in PHP?

1) json_encode()

2) json_encode_array()

3) array_to_json()

4) encode_json()

Answer : Option 1