PHP - Interview Series - 2

- What is the difference between array_key_exists() and in_array() functions in PHP?
Answer: The array_key_exists() function is used to check if a specific key exists in an array, while the in_array() function is used to check if a specific value exists in an array.
- What is a PHP autoloader?
Answer: A PHP autoloader is a function that automatically loads classes as they are needed, without the need for manual loading.
- What is the use of the var_dump() function in PHP?
Answer: The var_dump() function is used to display structured information about a variable, including its type and value.
- What is a PHP trait?
Answer: A PHP trait is a reusable piece of code that can be used in multiple classes to add functionality.
- What is the use of the strtotime() function in PHP?
Answer: The strtotime() function is used to convert a string date into a Unix timestamp.
Example code:
date = "2023-04-29"; timestamp = strtotime($date);
- What is the use of the file_get_contents() function in PHP?
Answer: The file_get_contents() function is used to read the contents of a file into a string.
Example code:
$data = file_get_contents("example.txt");
- What is the difference between single-quoted and double-quoted strings in PHP?
Answer: Single-quoted strings in PHP do not interpret escape sequences or variables, while double-quoted strings do.
- What is the use of the array_map() function in PHP?
Answer: The array_map() function is used to apply a callback function to each element of an array.
Example code:
$numbers = array(1, 2, 3); $squares = array_map(function($n) { return $n * $n; }, $numbers);
- What is the use of the array_filter() function in PHP?
Answer: The array_filter() function is used to filter the elements of an array based on a callback function.
Example code:
$numbers = array(1, 2, 3, 4, 5); $even_numbers = array_filter($numbers, function($n) { return $n % 2 == 0; });
- What is the use of the array_reduce() function in PHP?
Answer: The array_reduce() function is used to apply a callback function to the elements of an array, and reduce it to a single value.
Example code:
$numbers = array(1, 2, 3, 4, 5); $product = array_reduce($numbers, function($acc, $n) { return $acc * $n; }, 1);
- What is the use of the array_slice() function in PHP?
Answer: The array_slice() function is used to extract a portion of an array.
Example code:
$numbers = array(1, 2, 3, 4, 5); $slice = array_slice($numbers, 2, 2);
- What is the use of the array_merge() function in PHP?
Answer: The array_merge() function is used to merge two or more arrays into a single array.
Example code:
$numbers1 = array(1, 2, 3); $numbers2 = array(4, 5, 6); $merged_numbers = array_merge($numbers1, $numbers2);