Learn PHP MCQs

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

Which function is used to get the current script's directory in PHP?

1) dirname(__FILE__)

2) getcwd()

3) __DIR__

4) getdirname()

Answer : Option 1

How do you generate a random floating-point number in PHP?

1) rand()/getrandmax()

2) random_float()

3) generate_float()

4) float_rand()

Answer : Option 1

Which function is used to create an array from variables in PHP?

1) compact()

2) array()

3) extract()

4) create_array()

Answer : Option 1

How do you remove all trailing whitespace from a string in PHP?

1) rtrim()

2) ltrim()

3) trim()

4) strip()

Answer : Option 1

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

1) Converts the first character of a string to uppercase

2) Converts all characters of a string to uppercase

3) Capitalizes every word in a string

4) None of the above

Answer : Option 1

How do you remove the last character from a string in PHP?

1) substr($string, 0, -1)

2) trim($string)

3) cut($string)

4) None of the above

Answer : Option 1

Which function is used to delete a file in PHP?

1) unlink()

2) remove()

3) delete()

4) unlink_file()

Answer : Option 1

How do you redirect a user to another page in PHP?

1) header("Location: new_page.php")

2) redirect("new_page.php")

3) goto("new_page.php")

4) send("new_page.php")

Answer : Option 1

What is the output of `echo 5 . "5";` in PHP?

1) 55

2) 10

3) Error

4) 5

Answer : Option 1

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

1) empty()

2) strlen()

3) is_null()

4) is_empty()

Answer : Option 1