PHP Questions and Answers Part-18

1. Which one of the following function is capable of reading a file into an array?
a) file()
b) arrfile()
c) arr_file()
d) file_arr()

Answer: a
Explanation: The function file() will read the entire file into an array.

2. Which one of the following function is capable of reading a file into a string variable?
a) file_contents()
b) file_get_contents()
c) file_content()
d) file_get_content()

Answer: b
Explanation: The function file_get_contents() reads a file into a string. This is the preferred way to read the contents of a file into a string as it will use memory mapping techniques.

3. Which one of the following function is capable of reading a specific number of characters from a file?
a) fgets()
b) fget()
c) fileget()
d) filegets()

Answer: a
Explanation: The function fgets() will return a line from an open file. This stops returning on a new line, at the specified length, or at EOF, whichever comes first. Its prototype is string fgets(resource handle [, int length]). If the optional length parameter is omitted, 1024 character is assumed.

4. Which one of the following function operates similarly to fgets(), except that it also strips any HTML and PHP tags form the input?
a) fgetsh()
b) fgetsp()
c) fgetsa()
d) fgetss()

Answer: d
Explanation: The function fgetss() returns a line, with HTML and PHP tags removed, from an open file. This function operates similarly to fgets(), except that it also strips any HTML and PHP tags form the input.

5. Which one of the following function outputs the contents of a string variable to the specified resource?
a) filewrite()
b) fwrite()
c) filewrites()
d) fwrites()

Answer: b
Explanation: The function fwrite() writes to an open file. This will stop at the end of the file or when it reaches the specified length, whichever comes first.

6. Which function sets the file filename last-modified and last-accessed times?
a) sets()
b) set()
c) touch()
d) touched()

Answer: c
Explanation: The function touch() will set the access and modification time of the specified file. Syntax is touch(filename, time, atime).

7. Which function is useful when you want to output the executed command result?
a) out_cmm()
b) out_system()
c) cmm()
d) system()

Answer: d
Explanation: The function system() in PHP is same as the C version of the function as in that it executes the given command and outputs the result. This function also tries to automatically flush the web server’s output buffer after each line of output if PHP is running as a server module.

8. Which one of the following function reads a directory into an Array?
a) scandir()
b) readdir()
c) scandirectory()
d) readdirectory()

Answer: a
Explanation: It returns an array consisting of files and directories found in directory or returns FALSE on error.

9. What will be the output of the following PHP code?
<?php
echo (checkdate(4,31,2010) ? 'Valid' : 'Invalid');
?>
a) TRUE
b) FALSE
c) Valid
d) Invalid

Answer: d
Explanation: The function checkdate() is used to validate a Gregorian date. In the program, April has 30 days and the above date is 31 therefore Invalid is returned.

10. The date() function returns ___ representation of the current date and/or time.
a) Integer
b) String
c) Boolean
d) Float

Answer: b
Explanation: The function date() is used to format a local date and time, and it will return the formatted date string. The syntax of this function is string date(string format [, int timestamp]).