1. Question: What will be the output of the following script?
    <?php
    $result = '';
    function glue ($val){
      global $result;
      $result .= $val;
    }
    $array = array ('a', 'b', 'c', 'd');
    array_walk ($array, 'glue');
    echo $result;
    ?>

    A
    abcd

    B
    glue

    C
    Array

    D
    abcdabcd

    E
    none

    Note: The array_walk function executes a given callback function for every element of an array. Therefore, this script will cause the glue function to concatenate all the elements of the array and output abcd.
    1. Report
  2. Question: What will the following script output?
    <?php
    $array = array (1, 2, 3, 5, 8, 13, 21, 34, 55);
    $sum = 0;
    for ($i = 0; $i < 5; $i++) {
      $sum += $array[$array[$i]];
    }
    echo $sum;
    ?>

    A
    78

    B
    19

    C
    NULL

    D
    5

    E
    0

    Note: This question is designed to test your ability to analyze a complex script more than your understanding of arrays. You may think it too convoluted—but we’ve all been faced with the not-so-pleasant task of debugging someone else’s code, and compared to some of the scripts we’ve seen, this is actually quite simple. The script simply cycles through the for loop five times, each time adding to $sum the value of the element of $array whose key is equal to the value of the element of $array whose key is equal to $i. It might sound a bit like a high-tech variation of “how much wood would a wood chuck chuck,” but if you step through the code manually, you’ll find that, when $i is zero, then $array[$array[$i]] becomes $array[$array[0]], or $array[1], that is, 2. Applied to all the iterations of the for loop, the resulting total is 78.
    1. Report
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
Copyright © 2024. Powered by Intellect Software Ltd