PHP Multiple Choice Questions on “In-Built Functions in PHP”.
1. Which of the following PHP functions accepts any number of parameters?
a) func_get_argv()
b) func_get_args()
c) get_argv()
d) get_argc()
Answer: b
Clarification: func_get_args() returns an array of arguments provided. One can use func_get_args() inside the function to parse any number of passed parameters. Here is an example:
-
function foo()
-
{
-
$args = func_get_args();
-
foreach ($args as $k => $v)
-
{
-
echo "arg".($k+1).": $vn";
-
}
-
}
-
foo();
-
/* will print nothing */
-
-
foo("Hello");
-
/* will print Hello */
-
-
foo("Hello","World","Bye");
-
/* will print Hello World Bye */
2. Which one of the following PHP functions can be used to find files?
a) glob()
b) file()
c) fold()
d) get_file()
Answer: a
Clarification: The function glob() returns an array of filenames or directories which matches a specified pattern. The function returns an array of files/directories, or it will return FALSE on failure. Here is an example-
-
// get all php files AND txt files
-
$files = glob('*.{php,txt}', GLOB_BRACE);
-
print_r($files);
-
/* output looks like:
-
Array
-
(
-
[0] => phptest.php
-
[1] => pi.php
-
[2] => post_output.php
-
.
-
.
-
.
-
)
3. Which of the following PHP functions can be used to get the current memory usage?
a) get_usage()
b) get_peak_usage()
c) memory_get_usage()
d) memory_get_peak_usage()
Answer: c
Clarification: memory_get_usage() returns the amount of memory, in bytes, that’s currently being allocated to the PHP script. We can set the parameter ‘real_usage’ to TRUE to get total memory allocated from system, including unused pages. If it is not set or FALSE then only the used memory is reported. To get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.
4. Which of the following PHP functions can be used for generating unique ids?
a) uniqueid()
b) id()
c) md5()
d) mdid()
Answer: a
Clarification: The function uniqueid() is used to generate a unique ID based on the microtime (current time in microseconds). The ID generated from the function uniqueid() is not optimal, as it is based on the system time. To generate an ID which is extremely difficult to predict we can use the md5() function.
5. Which one of the following functions can be used to compress a string?
a) zip_compress()
b) zip()
c) compress()
d) gzcompress()
Answer: d
Clarification: The function gzcompress() compresses the string using the ZLIB data format. One can achieve upto 50% size reduction using this function. The gzuncompress() function is used to uncompress the string.
6. What will be the output of the following PHP code?
a) 1
b) 2
c) 3
d) 4
Answer: d
Clarification: The chr() function returns a character from the specified ASCII value. We can specify ASCII value in decimal, octal, or hex values. The Octal values are defined as a leading 0, while hex values are defined as a leading 0x. Since the ASCII value of 4 is 52, thus 4 was displayed.
7. What will be the output of the following PHP code?
a) 106
b) 103
c) 104
d) 209
Answer: c
Clarification: The ord() function returns the ASCII value of the first character of a string. The ASCII value of h is 104, thus 104 was displayed.
8. 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.
9. 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.
10. 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.