Advanced PHP Questions and Answers on “Functions – 3”.
1. What will be the output of the following PHP code?
-
-
$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: d
Clarification: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).
2. What will be the output of the following PHP code?
-
-
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
Clarification: The str_pad() function pads a string with a specified number of characters.
3. What will be the output of the following PHP code?
-
-
$str = "Hello World"
-
echo wordwrap($str,5,"
n"); -
?>
a) Hello World
b)
Hello World
c)
Hell o wo rld
d) World
Answer: b
Clarification: The wordwrap() function wraps a string into new lines when it reaches a specific length.
4. What will be the output of the following PHP code?
-
-
echo ucwords("i love my country");
-
?>
a) I love my country
b) i love my Country
c) I love my Country
d) I Love My Country
Answer: d
Clarification: The ucwords() function converts the first character of each word in a string to uppercase.
5. What will be the output of the following PHP code?
-
-
echo lcfirst("welcome to India");
-
?>
a) welcome to India
b) welcome to india
c) Welcome to India
d) Welcome to india
Answer: a
Clarification: The lcfirst() function converts the first character of a string to lowercase.
6. What will be the output of the following PHP code?
-
-
echo hex2bin("48656c6c6f20576f726c6421");
-
?>
a) Hello World!
b) welcome to india
c) This is PHP!
d) MCQ questons
Answer: a
Clarification: The hex2bin() function converts a string of hexadecimal values to ASCII characters.
7. What will be the output of the following PHP code?
-
-
$str = addslashes('What does "yolo" mean?');
-
echo($str);
-
?>
a) What does /”yolo/” mean?
b) What does \”yolo\” mean?
c) What does ”yolo” mean?
d) What does ”yolo” mean?
Answer: c
Clarification: The addslashes() function returns a string with backslashes in front of predefined characters.
8. What will be the output of the following PHP code?
-
-
$str = "Hello world. It's a beautiful day.";
-
print_r (explode(" ",$str));
-
?>
a) Array ( [0] => Hello [0] => world. [0] => It’s [0] => a [0] => beautiful [0] => day. )
b) Array ( [0] => Hello [1] => world. [2] => It’s [3] => a [4] => beautiful [5] => day. )
c) Hello world. It’s a beautiful day
d) Array ( [1] => Hello [2] => world. [3] => It’s [4] => a [5] => beautiful [6] => day. )
Answer: b
Clarification: The explode() function breaks a string into an array.
9. What will be the output of the following PHP code?
-
-
echo strtr("Hilla Warld","ia","eo");
-
?>
a) Hilla Warld
b) Hello World
c) ia
d) eo
Answer: b
Clarification: The strtr() function translates certain characters in a string.
10. What will be the output of the following PHP code?
-
-
echo stripos("I love php, I love php too!","PHP");
-
?>
a) 3
b) 7
c) 8
d) 10
Answer: b
Clarification: The stripos() function finds the position of the first occurrence of a string inside another string.
To practice advanced questions and answers on all areas of PHP,
