1. Question: The _____ function is used to read a single line from a file and is used when dealing with text files. For reading binary data or other specific segments of a file, you should use the _____ function instead.

    A
    fgets(), fseek()

    B
    fread(), fgets()

    C
    fputs(), fgets()

    D
    fgets(), fread()

    E
    fread(), fseek()

    Note: Although it is possible to specify a maximum length when calling it, the fgets function defaults to reading a single line from the given file resource and is primarily used for text files. The fread function, on the other hand, is used primarily to read binary data. That makes answer D correct.
    1. Report
  2. Question: Although file resources will automatically be closed at the end of a request in PHP, you can close them explicitly by calling the _______ function. Your Answer: _________

    A
    fclose()

    B
    fexit()

    C
    close()

    D
    fileclose()

    E
    file_close()

    Note: The correct answer is the fclose function, which closes an open file resource.
    1. Report
  3. Question: Consider the following PHP script, which reads a file, line-by-line, from a text file. Which function call should be inserted in place of the question marks in order for the script to function correctly?
    <?php
    $file = fopen("test", "r");
    while(!feof($file)) {
       echo ????????????;
    }
    fclose($file);
    ?>

    A
    file_get_contents($file)

    B
    file($file)

    C
    read_file($file)

    D
    fgets($file)

    E
    fread($file)

    Note: The fgets function is used to read a single newline-terminated string from a file. Therefore, Answer D is correct, as none of the remaining options provide any valid alternative.
    1. Report
  4. Question: Which of the following techniques will guarantee a lock safe from any race condition?

    A
    Using flock() to lock the desired file

    B
    fopen()’ing a file in the operating system’s temporary directory

    C
    Creating a temporary file with tempnam()

    D
    Using mkdir() to create a directory and use it as a lock reference

    E
    Using tmpfile() to create a temporary file

    Note: The correct answer is D. This is a very tough question, and one you’re not likely to find in the real exam—but that’s why you’re reading this book! You must remember that flock() uses a “cooperative” locking mechanism with one big assumption: that all other processes that want to access your file will also use flock(). If they don’t, race conditions can arise and the lock is not guaranteed. Curiously, creating a directory with the mkdir function is guaranteed to be an atomic operation, meaning that only one process at any given time can perform it. Therefore, you can create a temporary directory and “hold it” until you have finished your I/O operations.
    1. Report
  5. Question: Which of the following functions retrieve the entire contents of a file in such a way that it can be used as part of an expression? (Choose 2)

    A
    file_get_contents()

    B
    fgets()

    C
    fopen()

    D
    file()

    E
    readfile()

    Note: Only the file_get_contents and file functions retrieve the entire contents of a file and, therefore, the correct answers are A and D. The readfile function does read the entire contents of a file, but sends them directly to the output buffer, thus making it impossible to use them in an expression (short of using output buffering to capture the file’s contents).
    1. Report
  6. Question: How would you parse the contents of a multi-line text file formatted using a fixed pattern without preloading its contents into a variable and then processing them in memory?

    A
    Using file() to break it up into an array

    B
    Using sscanf()

    C
    Using fscanf()

    D
    Using fgets()

    E
    Using fnmatch()

    Note: The fscanf function can be used to parse the contents of a file according to a fixed predefined pattern; therefore, the correct answer is C. The sscanf function only operates on strings.
    1. Report
  7. Question: Consider the following script. What will the file myfile.txt contain at the end of its execution?
    <?php
    $array = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $f = fopen ("myfile.txt", "r");
    for ($i = 0; $i < 50; $i++) {
      fwrite ($f, $array[rand(0, strlen ($array) - 1)]);
    }
    ?>

    A
    Nothing, because $array is not an actual array but a string.

    B
    A random sequence of 49 characters.

    C
    A random sequence of 50 characters.

    D
    A random sequence of 41 characters.

    E
    Nothing, or the file will not exist, and the script will output an error

    Note: The correct answer is E. Note how the file is being opened with the r parameter, which indicates that we want to use the file for reading. Therefore, if the file does not exist, PHP will output an error complaining that it cannot be found. If it does exist, the call to fopen() will be successful, but the subsequent fwrite() operations will fail due to the file having been opened in the wrong way. If we were to specify w instead of r, the script would run successfully and myfile.txt would contain a sequence of fifty random characters (remember that the characters of a string can be accessed as if they were elements of an array, just like in C).
    1. Report
  8. Question: What does the built-in delete function do?

    A
    It deletes a file

    B
    It deletes a directory

    C
    It unsets a variable

    D
    It removes a database row

    E
    This function does not exist!

    Note: Answer E is correct. There is no function called delete() in PHP. Files are deleted with unlink(), while directories are deleted with rmdir(). Database rows are deleted using the DELETE SQL statement (not a PHP function) and, finally, variables are unset using unset().
    1. Report
  9. Question: Consider the following script. Which PHP function best approximates its behaviour?
    <?php
    function my_funct ($file_name, $data)
    {
        $f = fopen ($file_name, 'w');
        fwrite ($f, $data);
        fclose ($f);
    }
    ?>
    [

    A
    file_get_contents()

    B
    file_put_contents()

    C
    There is no equivalent function in PHP

    D
    file()

    E
    fputs()

    Note: The script in this question best approximates the way file_put_contents() works; however, this function does not exist in PHP 4, having been introduced with PHP 5. Therefore, Answer C is correct.
    1. Report
  10. Question: What should you do if your script is having problem recognizing file endings from a text file saved on a platform different from the one you’re reading it on?

    A
    Change the auto_detect_line_endings INI setting

    B
    Use a regular expression to detect the last letter of a line

    C
    Use fpos()

    D
    Use ftok()

    E
    Read the file one character at a time

    Note: The auto_detect_line_endings php.ini setting was introduced in PHP 4.3.0 to make the system attempt to automatically detect the type of line endings used when saving a text file. Therefore, Answer A is correct.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd