PHP - MCQ Questions and Answers

Test your knowledge of PHP with these interactive multiple-choice questions.

« First 2 3 4 5 Last »
« Previous Page Next Page »

41. What is the output of echo 0 == 'false';?

  • a) 1 (true)
  • b) 0 (false)
  • c) Error
  • d) 'false'
Answer: A - Loose comparison converts 'false' to 0, making 0 == 0 true.

42. Which function returns the current Unix timestamp?

  • a) time()
  • b) date()
  • c) now()
  • d) timestamp()
Answer: A - time() returns current Unix timestamp (seconds since Jan 1 1970).

43. What is the correct way to declare a namespace in PHP?

  • a) namespace MyProject;
  • b) use MyProject;
  • c) \namespace MyProject;
  • d) #namespace MyProject
Answer: A - Namespaces are declared with the namespace keyword at the file's top.

44. Which function removes duplicate values from an array?

  • a) array_unique()
  • b) array_distinct()
  • c) remove_duplicates()
  • d) unique_array()
Answer: A - array_unique() removes duplicate values (keeps first occurrence).

45. What is the correct way to handle exceptions in PHP?

  • a) try/catch blocks
  • b) on_error()
  • c) exception_handle()
  • d) @ operator
Answer: A - Exceptions are handled using try/catch blocks.

46. Which function reverses a string?

  • a) strrev()
  • b) reverse()
  • c) str_reverse()
  • d) string_reverse()
Answer: A - strrev() reverses a string (e.g., "hello" → "olleh").

47. What does the === operator check?

  • a) Value and type equality
  • b) Only value equality
  • c) Only type equality
  • d) Reference equality
Answer: A - === checks both value and type (strict comparison).

48. Which function converts a string to an array?

  • a) explode()
  • b) str_split()
  • c) split()
  • d) Both A and B
Answer: D - Both explode() (by delimiter) and str_split() (by length) convert strings to arrays.

49. What is the correct way to include a file in PHP?

  • a) include 'file.php';
  • b) require 'file.php';
  • c) include_once 'file.php';
  • d) All of the above
Answer: D - All are valid, with different behaviors on failure and multiple inclusions.

50. Which function checks if a variable is an array?

  • a) is_array()
  • b) isArray()
  • c) typeOf()
  • d) gettype()
Answer: A - is_array() checks if a variable is an array.

51. What is the correct way to create a cookie?

  • a) setcookie(name, value, expire);
  • b) create_cookie(name, value);
  • c) cookie(name, value, expire);
  • d) make_cookie(name, value);
Answer: A - setcookie() is used to create cookies in PHP.

52. Which function returns the keys of an array?

  • a) array_keys()
  • b) keys()
  • c) get_keys()
  • d) array_key_list()
Answer: A - array_keys() returns all keys or a subset of keys from an array.

53. What is the correct way to create a class in PHP?

  • a) class MyClass { }
  • b) object MyClass { }
  • c) new class MyClass { }
  • d) create class MyClass { }
Answer: A - Classes are declared with the class keyword.

54. Which function formats a local time/date?

  • a) date()
  • b) time()
  • c) format_date()
  • d) localtime()
Answer: A - date() formats a timestamp (defaults to current time).

55. What is the correct way to create an object?

  • a) new ClassName()
  • b) create ClassName()
  • c) object ClassName()
  • d) ClassName.create()
Answer: A - Objects are instantiated with the new keyword.

56. Which function checks if a file exists?

  • a) file_exists()
  • b) is_file()
  • c) exists()
  • d) check_file()
Answer: A - file_exists() checks whether a file or directory exists.

57. What is the correct way to sort an array in descending order?

  • a) rsort()
  • b) arsort()
  • c) krsort()
  • d) All of the above
Answer: D - All sort in descending order (rsort: values, arsort: associative by values, krsort: associative by keys).

58. Which function returns the number of array elements?

  • a) count()
  • b) sizeof()
  • c) length()
  • d) Both A and B
Answer: D - Both count() and sizeof() return the number of elements (aliases).

59. What is the correct way to read a JSON file?

  • a) json_decode(file_get_contents('file.json'))
  • b) read_json('file.json')
  • c) file_read('file.json', 'json')
  • d) json_read('file.json')
Answer: A - file_get_contents() reads the file, json_decode() parses JSON.

60. Which function executes a prepared PDO statement?

  • a) execute()
  • b) run()
  • c) query()
  • d) pdo_exec()
Answer: A - The execute() method runs a prepared PDO statement.
« First 2 3 4 5 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.