PHP PHP 4 — Questions and Answers
Question 1: Which PHP function is used to execute a regular expression and store captured groups?
- preg_match() (Correct answer)
- regex_exec()
- str_match()
- regexp_search()
Correct answer: preg_match()
preg_match() performs a regular expression match and populates an array with captured groups.
Question 2: What is the default visibility of class properties and methods in PHP if no modifier is specified?
- private
- protected
- public (Correct answer)
- internal
Correct answer: public
In PHP, members declared without an access modifier default to public visibility.
Question 3: Which PHP function formats a number with thousands separators and decimal points?
- format_number()
- number_format() (Correct answer)
- sprintf('%f')
- round()
Correct answer: number_format()
number_format() formats a number with grouped thousands and allows specifying decimal places and separator characters.
Question 4: What does PHP's list() construct do?
- Creates an ordered HTML list
- Assigns values from an array to variables (Correct answer)
- Lists all global variables
- Returns array keys as a list
Correct answer: Assigns values from an array to variables
list() (or its shorthand []) is used to assign array values to multiple variables in one statement.
Question 5: In PHP, which loop guarantees that the loop body executes at least once?
- for
- foreach
- while
- do...while (Correct answer)
Correct answer: do...while
The do...while loop checks its condition after executing the body, so the body runs at least one time.
Question 6: What PHP function is used to retrieve the last error that occurred?
- get_last_error()
- error_get_last() (Correct answer)
- last_error()
- php_error()
Correct answer: error_get_last()
error_get_last() returns an associative array describing the last error that occurred, or null if there was no error.
Question 7: Which PHP 8 feature allows you to name arguments in any order when calling a function?
- Spread operator
- Named arguments (Correct answer)
- Match expression
- Constructor promotion
Correct answer: Named arguments
Named arguments, introduced in PHP 8.0, let you pass values to a function by specifying the parameter name, independent of order.
Which PHP function is used to execute a regular expression and store captured groups?