Learn PHP MCQs

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

Which of the following is used to perform a regular expression search and replace in PHP?

1) preg_replace()

2) replace_regex()

3) regex_replace()

4) preg_search()

Answer : Option 1

How do you handle exceptions in PHP?

1) try-catch block

2) try-finally block

3) catch-exception block

4) try-catch-finally block

Answer : Option 1

How do you remove the first element from an array in PHP?

1) array_shift()

2) array_unshift()

3) array_pop()

4) array_remove()

Answer : Option 1

What is the default port for MySQL when connecting via PHP?

1) 3306

2) 3307

3) 3308

4) 3309

Answer : Option 1

Which function is used to include a PHP file within another PHP file?

1) include()

2) require()

3) Both A and B

4) None of the above

Answer : Option 3

What is the default extension of a PHP file?

1) .php3

2) .php

3) .ph

4) .phtml

Answer : Option 2

Which of the following is the correct way to start a PHP block?

1)

2)

3)

4) <%

Answer : Option 1

How can you access the global variable within a function in PHP?

1) global $var;

2) $GLOBALS['var'];

3) Both A and B

4) None of the above

Answer : Option 3

Which of the following is the correct way to add a comment in PHP?

1) // Comment

2) /* Comment */

3) # Comment

4) All of the above

Answer : Option 4

How do you find the length of a string in PHP?

1) strlen()

2) length()

3) strlength()

4) count()

Answer : Option 1