Learn PHP MCQs

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

How can you start a PHP session?

1) session_start();

2) start_session();

3) session();

4) begin_session();

Answer : Option 1

Which function can be used to remove all leading and trailing whitespace in a string?

1) trim()

2) ltrim()

3) rtrim()

4) strip()

Answer : Option 1

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

1) isset()

2) is_set()

3) defined()

4) isset_var()

Answer : Option 1

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

1) Returns the data type of a variable

2) Checks if a variable is an integer

3) Converts a variable to a string

4) None of the above

Answer : Option 1

How do you convert a value to a string in PHP?

1) strval()

2) string()

3) convert_to_string()

4) stringify()

Answer : Option 1

Which function is used to stop the execution of a PHP script?

1) exit()

2) die()

3) end()

4) Both A and B

Answer : Option 4

How do you create an indexed array in PHP?

1) $array = array("A", "B", "C");

2) $array = array("key1" => "A", "key2" => "B", "key3" => "C");

3) $array = array("A", "B", "C" => "D");

4) None of the above

Answer : Option 1

Which of the following is a PHP magic method?

1) __construct()

2) __toString()

3) __get()

4) All of the above

Answer : Option 4

How do you connect to a MySQL database using PHP?

1) mysqli_connect()

2) mysql_connect()

3) connect_db()

4) db_connect()

Answer : Option 1

Which of the following is the correct way to open a file for reading in PHP?

1) fopen("file.txt", "r");

2) open("file.txt", "read");

3) open("file.txt", "r");

4) fopen("file.txt", "read");

Answer : Option 1