Explore fundamental concepts in manipulating arrays and strings with PHP. Assess your understanding of basic functions, common operations, and simple syntax for working with arrays and strings in practical scenarios.
Which PHP function returns the number of elements in the following array: array('dog', 'cat', 'bird')?
Explanation: The count() function is used to determine the number of elements in an array in PHP. length() is not a built-in PHP function for arrays. sizeof() does work similarly but count() is more standard and widely used. elements() is not a recognized PHP function for counting array items.
Given an array $colors = array('red', 'green', 'blue'), which function checks if 'green' exists in the array?
Explanation: The in_array() function verifies if a specific value is present in an array, returning true if found. array_has() is not a native PHP function. find() and seek() are not valid for checking values in arrays in PHP. Only in_array() accomplishes this task directly.
Which PHP function returns the number of characters in the string 'elephant'?
Explanation: strlen() is the correct PHP function for counting the number of characters in a string. string_length() and length() are not built-in PHP functions. count() is used for arrays, not strings, making strlen() the only valid choice.
To combine the array array('a', 'b', 'c') into the string 'a-b-c', which function and delimiter would you use?
Explanation: implode() is the function used to join array elements with a string delimiter. combine(), glue(), and joiner() are not valid PHP functions for this purpose. While join() also works, it is an alias for implode(), but joiner() is not recognized.
Which function splits the string 'red,green,blue' into an array using the comma as a separator?
Explanation: explode() splits a string by a specified separator into an array and is the correct PHP function. split() has been deprecated and is not recommended. cut() and break() do not exist in PHP for string splitting.
How would you add the value 'banana' to the end of the array $fruits in PHP?
Explanation: array_push() adds elements to the end of an array in PHP. add() and push() are not standard array functions. $fruits-u003Eappend('banana') is object-oriented syntax and not applicable for standard PHP arrays.
If you want to find the position of 'cat' in the string 'wildcat', which PHP function should you use?
Explanation: strpos() locates the numeric position of a substring within another string in PHP. findstr(), search(), and pos() are not defined PHP functions, so only strpos() is valid for this purpose.
Which function will reverse the order of elements in the array array(1, 2, 3) in PHP?
Explanation: array_reverse() reverses the order of elements in an array. reverse(), flip(), and invert() are not recognized PHP array functions. flip() in PHP exists as array_flip(), but it swaps keys and values, not order.
Which PHP function converts the string 'Hello World' to all lowercase letters?
Explanation: strtolower() is the standard PHP function that converts a string to lowercase. tolower(), stringlower(), and lowercase() are not valid PHP functions for changing case.
Which function removes leading and trailing spaces from the string ' PHP '?
Explanation: trim() removes whitespace from the beginning and end of a string in PHP. strip(), cut(), and erase() are not appropriate PHP functions for this functionality. Only trim() is specifically designed for this task.