Learn PHP MCQs

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

What is the correct way to write a multi-line string in PHP?

1) Using heredoc syntax

2) Using a single quoted string with newline characters

3) Using a double quoted string with newline characters

4) All of the above

Answer : Option 4

How do you check if a variable is a string in PHP?

1) is_string()

2) check_string()

3) is_var_string()

4) isstr()

Answer : Option 1

Which function is used to convert all applicable characters to HTML entities in PHP?

1) htmlspecialchars()

2) htmlentities()

3) htmlconvert()

4) htmlencode()

Answer : Option 2

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

1) Frees all session variables

2) Destroys the session

3) Ends the session

4) Unsets a specific session variable

Answer : Option 1

How do you get the first character of a string in PHP?

1) substr($string, 0, 1)

2) strfirst($string)

3) charat($string, 0)

4) substr($string, 1, 1)

Answer : Option 1

Which of the following functions can be used to sanitize a string in PHP?

1) filter_var()

2) htmlentities()

3) htmlspecialchars()

4) All of the above

Answer : Option 4

How do you increment a variable by 1 in PHP?

1) $var++

2) $var = $var + 1

3) $var += 1

4) All of the above

Answer : Option 4

Which function would you use to get the last character of a string in PHP?

1) substr($string, -1)

2) strlast($string)

3) charat($string, -1)

4) substr($string, strlen($string) - 1, 1)

Answer : Option 1

Which of the following is used to get the current timestamp in PHP?

1) time()

2) date()

3) now()

4) timestamp()

Answer : Option 1

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

1) Splits a string by a specified delimiter

2) Combines elements into a string

3) Removes a substring from a string

4) Concatenates two strings

Answer : Option 1