21. Which PHP function is used to open a file for writing?
- a) fopen() with 'w' mode
- b) file_open()
- c) open_file()
- d) file_write()
Answer: A - fopen() with 'w' mode opens a file for writing (creates if doesn't exist, truncates if exists).
22. What is the correct way to create a PHP function?
- a) function myFunction() { }
- b) create function myFunction() { }
- c) new function myFunction() { }
- d) def myFunction() { }
Answer: A - PHP functions are declared with the function keyword followed by the name and parentheses.
23. Which superglobal variable contains data from POST requests?
- a) $_POST
- b) $_GET
- c) $_REQUEST
- d) $_DATA
Answer: A - $_POST contains data sent via HTTP POST method.
24. What does PHP's PDO stand for?
- a) PHP Data Objects
- b) PHP Database Operations
- c) Prepared Data Objects
- d) PHP Data Orientation
Answer: A - PDO stands for PHP Data Objects, a database access layer.
25. Which function converts a string to lowercase in PHP?
- a) strtolower()
- b) strtoupper()
- c) lowercase()
- d) str_lower()
Answer: A - strtolower() converts a string to lowercase.
26. What is the correct way to end a PHP statement?
- a) ; (semicolon)
- b) : (colon)
- c) . (period)
- d) , (comma)
Answer: A - PHP statements must end with a semicolon.
27. Which function returns the length of a string?
- a) strlen()
- b) count()
- c) sizeof()
- d) length()
Answer: A - strlen() returns the length of a string in bytes.
28. What is the correct way to create a constant in PHP?
- a) define("CONSTANT", "value")
- b) const CONSTANT = "value"
- c) $constant = "value"
- d) Both A and B
Answer: D - Both define() and const can create constants (const at compile time, define() at runtime).
29. Which operator is used for concatenation in PHP?
- a) . (dot)
- b) + (plus)
- c) & (ampersand)
- d) | (pipe)
Answer: A - The dot operator concatenates strings in PHP.
30. What will echo (int) '5.5'; output?
- a) 5
- b) 5.5
- c) '5.5'
- d) 6
Answer: A - Casting to int truncates the decimal portion without rounding.
31. Which function sends raw HTTP headers?
- a) header()
- b) send_headers()
- c) headers_send()
- d) http_header()
Answer: A - header() is used to send raw HTTP headers (must be called before any output).
32. What does the __DIR__ constant contain?
- a) Directory of the current file
- b) Document root directory
- c) Directory of the included file
- d) Temporary directory path
Answer: A - __DIR__ contains the directory of the current file (equivalent to dirname(__FILE__)).
33. Which function returns the current date and time?
- a) date()
- b) time()
- c) now()
- d) getdate()
Answer: A - date() formats the current timestamp (default) or a specified timestamp.
34. What is the correct MySQLi method to prepare a statement?
- a) prepare()
- b) query()
- c) stmt_prepare()
- d) mysqli_prepare()
Answer: A - prepare() method of mysqli object creates a prepared statement.
35. Which function converts special characters to HTML entities?
- a) htmlspecialchars()
- b) htmlentities()
- c) escape_html()
- d) convert_entities()
Answer: A - htmlspecialchars() converts &, ", ', <, and > to HTML entities.
36. What is the correct way to start a session?
- a) session_start()
- b) start_session()
- c) session_begin()
- d) init_session()
Answer: A - session_start() creates a session or resumes the current one.
37. Which function returns the last error that occurred?
- a) error_get_last()
- b) last_error()
- c) get_error()
- d) error_last()
Answer: A - error_get_last() returns the last error that occurred.
38. What does the __CLASS__ constant return?
- a) Current class name
- b) Parent class name
- c) Namespace of the class
- d) Class file path
Answer: A - __CLASS__ returns the name of the current class.
39. Which function converts an array to a JSON string?
- a) json_encode()
- b) json_serialize()
- c) array_to_json()
- d) serialize()
Answer: A - json_encode() returns the JSON representation of a value.
40. What is the correct way to destroy a session?
- a) session_destroy()
- b) session_end()
- c) session_abort()
- d) session_close()
Answer: A - session_destroy() destroys all data registered to a session.