PHP Questions & Answers – In-Built Functions in PHP

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses 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()
View Answer

Answer: b
Explanation: 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:

  1. function foo()
  2. {
  3.     $args = func_get_args();
  4.     foreach ($args as $k => $v)
  5.     {  
  6.        echo "arg".($k+1).": $v\n";  
  7.     }
  8. }
  9. foo();
  10. /* will print nothing */
  11.  
  12. foo("Hello");
  13. /* will print Hello */
  14.  
  15. foo("Hello","World","Bye");
  16. /* 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()
View Answer

Answer: a
Explanation: 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-

advertisement
advertisement
  1. // get all php files AND txt files  
  2.     $files = glob('*.{php,txt}', GLOB_BRACE);  
  3.     print_r($files);  
  4.     /* output looks like: 
  5.     Array 
  6.     ( 
  7.         [0] => phptest.php 
  8.         [1] => pi.php 
  9.         [2] => post_output.php
  10.         .
  11.         .
  12.         .
  13.     )

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()
View Answer

Answer: c
Explanation: 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.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. Which of the following PHP functions can be used for generating unique ids?
a) uniqueid()
b) id()
c) md5()
d) mdid()
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: d
Explanation: 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.
advertisement

6. What will be the output of the following PHP code?

  1.     <?php
  2.     echo chr(52);
  3.     ?>

a) 1
b) 2
c) 3
d) 4
View Answer

Answer: d
Explanation: 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.
advertisement

7. What will be the output of the following PHP code?

  1.     <?php
  2.     echo ord ("hi");
  3.     ?>

a) 106
b) 103
c) 104
d) 209
View Answer

Answer: c
Explanation: 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?

  1.     <?php
  2.     $str = "Hello World";
  3.     echo wordwrap($str,5,"<br>\n");    
  4.     ?>

a) Hello World
b)

   Hello 
   World

c)

   Hell
   o wo
   rld 

d) World
View Answer

Answer: b
Explanation: 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?

  1.     <?php
  2.     echo ucwords("i love my country");
  3.     ?>

a) I love my country
b) i love my Country
c) I love my Country
d) I Love My Country
View Answer

Answer: d
Explanation: 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?

  1.     <?php
  2.     echo lcfirst("welcome to India");
  3.     ?>

a) welcome to India
b) welcome to india
c) Welcome to India
d) Welcome to india
View Answer

Answer: a
Explanation: The lcfirst() function converts the first character of a string to lowercase.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.