PHP Questions and Answers Part-17

1. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular expressions.
a) 7
b) 8
c) 9
d) 10

Answer: b
Explanation: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(), preg_replace(), preg_replace_callback(), and preg_split().

2. What will be the output of the following PHP code?
<?php
$foods = array("pasta", "steak", "fish", "potatoes");
$food = preg_grep("/^s/", $foods);
print_r($food);
?>
a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )

Answer: c
Explanation: This function is used to search an array for foods beginning with s.

3. Say we have two compare two strings which of the following function/functions can you use?
i) strcmp()
ii) strcasecmp()
iii) strspn()
iv) strcspn()
a) i) and ii)
b) iii) and iv)
c) only i)
d) i), ii), iii) and iv)

Answer: d
Explanation: All of the functions mentioned above can be used to compare strings in some or the other way.

4. Which one of the following functions will convert a string to all uppercase?
a) strtoupper()
b) uppercase()
c) str_uppercase()
d) struppercase()

Answer: a
Explanation: Its prototype follows string strtoupper(string str).

5. What will be the output of the following PHP code?
<?php
$title = "O'malley wins the heavyweight championship!";
echo ucwords($title);
?>
a) O’Malley Wins The Heavyweight Championship!
b) O’malley Wins The Heavyweight Championship!
c) O’Malley wins the heavyweight championship!
d) o’malley wins the heavyweight championship!

Answer: a
Explanation: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).

6. What will be the output of the following PHP code?
<?php
echo str_pad("Salad", 5)." is good.";
?>
a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good

Answer: d
Explanation: The str_pad() function pads a string with a specified number of characters.

7. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
a) explode()
b) implode()
c) concat()
d) concatenate()

Answer: b
Explanation: implode()

8. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
a) strlastpos()
b) strpos()
c) strlast()
d) strrpos()

Answer: d
Explanation: strrpos()

9. The filesize() function returns the file size in ___________
a) bits
b) bytes
c) kilobytes
d) gigabytes

Answer: b
Explanation: The function filesize() returns the size of the specified file and it returns the file size in bytes on success or FALSE on failure.

10. Which one of the following PHP function is used to determine a file’s last access time?
a) fileltime()
b) filectime()
c) fileatime()
d) filetime()

Answer: c
Explanation: The fileatime() function returns a file’s last access time in Unix timestamp format or FALSE on error.