Esquire Theme by Matthew Buchanan
Social icons by Tim van Damme

09

Jun

More PHP quizzes (Part 1)

I am at the Orange County Public Library in Orlando, FL reading “SAMS Teach Yourself PHP in 24 Hours” by Matt Zandstra. A PHP book
copyrighted in 2004. In this book there are many PHP exercises that are useful for those willing to take the ZEND Certification test.

Remember, it is in the easy questions that most of us tend to get wrong.

**************** The Building Blocks ****************


1) Which of the following variables names is not valid?

a) $a_value_submitted_by_a_user
b) $6666666xyz
c) $xvz6666666
d) $____counter_____
e) the first
f) file-name

  • Answer: The answer is a, e and f. A PHP variable many only begin with a letter or underscore and it may only have alphanumeric after that.


2) What will the following code fragment output?

$num = 33;
(boolean) $num;
print $num;

  • Answer: The answer is 33 even though the value was converted.


3) What will the following statement output?

print gettype(“4”);

  • Answer: string


4) What will be the output from the following code fragment?

$test_val = 5.4566;
settype($test_val, “integer”);
print $test_val;

  • Answer: It will print the number 5. Any information beyong the decimal point is lost.


5) Which of the following statements does not contain an expression?

4;
gettype(44);
5/12;

  • Answer: They are all expressions because they all resolve to values.


6) Which of the statements in question 5 contains an operator?

  • Answer: The 3rd one has a division operator.


7) What value will the following expression return, and what data type will the returned value be?

5 < 2

  • Answer: The expression will resolve to false, which is a boolean value.

**************** Control Structures ****************

1) Must a control structure’s test expression result in a Boolean value?

  • Answer: Yes. However, in the context of a test expression 0(zero), NULL(undefined variable), “”(empty string) will all result in false for the purpose of the test. All other values evaluate to true.


2) Must I always surround a code block in a control statement with brackets?

  • Answer: If it only consists of 1 line, you can omit the brackets. But it is good practice for readability to have brackets.


**************** Functions ****************


1) Apart from the global keyword, is there any way that a function can access and change global variables?

  • Answer: You can also access global variables anywhere in your scripts with a built-in associative array called $GLOBALS. To access a global variable called $test within a function, you could reference it as $GLOBALS[‘test’].


2) Can you include a function call within a string, as you can with a variable?

  • Answer: No. You must call funtions outside quotation marks.


3) True or false: If a function doesn’t require an argument, you can omit the parentheses in the function call.

  • Answer: false. You must always include the parentheses in your function calls, whether or not you are passing arguments to the function.


**************** Arrays ****************


1) I can discover the number of elements in an array, so should I use a for statement loop through an array?

  • Answer: You should be cautious of this technique. If you are not absolutely sure that the array you are reading is indexed by consecutively numbered keys, you might get unexpected results.


2) Which construct can you use to define an array?

  • Answer: array()


3) Without using a function, what would be the easiest way of adding the element “Susan” to the $users array ($users = array(“Harry”, “Bob”, “Sandy”);)?

  • Answer: $users[] = “Susan”;


4) Which function could you use to add the string “Susan” to the $users array?

  • Answer: array_push($users, ‘Susan’);


5) Which function would you use to merge two array?

  • Answer: array_merge();


6) How would you sort an associate array by its key?

  • Answer: ksort();


**************** Working with strings ****************

1) Is <pre> tags the best way for formatting plain text on the browser?

  • Answer: <pre> tags can be useful if you want to preserve plain-text formatting in an HTML context. If you want to output an entire text document to the browser, however, it is neater to tell the browser to format the entire output as plain text. You can do this with the header() function: Header(“Content-Type: Text/Plain”);


2) Which conversion specifier would you use with printf() to format an integer as a double? Write down the full syntax required
to convert the integer 33.

  • Answer: The conversion specifier f is used to format an integer as a double: printf(“%f”, 33);


3) How would you pad the conversion you effected in question 2 with zeroes so that the part before the decimal point is four
characters long?

  • Answer: You can pad the output from printf() with the padding specifier — that is, a space or a zero followed by a number representing the number of characters by which you want to pad: printf(“%04f”, 33);


4) How would you specify a precision of two decimal places for the floating-point number you have been formatting in the previous
question?

  • Answer: The precision specifier consists of a dot(.) followed by a number representing the precision you want to apply. It should be placed before the conversion specifier: printf(“%04.2f”, 33);


5) Which function would you use to determine the length of a string?

  • Answer: The strlen() funciton returns the length of a string.


6) Which function would you use to acquire the starting index of a substring within a string?

  • Answer: The substr() function returns the starting index of a substring.


7) Which function would you use to extract a substring from a string?

  • Answer: The substr() function extracts and returns a substring.


8) How might you remove white space from the beginning of a string?

  • Answer: The ltrim() function removes white space from the start of a string.


9) How would you convert a string to uppercase characters?

  • Answer: The strtoupper() function convert a string to uppercase characters.


10) How would you break up a delimited string into an array of substrings?

  • Answer: The explode() function splits up a string into an array.


**************** Objects ****************

1) What is the $this special variable?

  • Answer: Within a class, you sometimes need to call the class’s methods or access its properties. By combining the $this variable and the -> operator, you can do both. The $this variable is the handle a class is automatically given to refer to itself and to its components.


2) How would you choose a name for a constructor method?

  • Answer: A constructor must either take the name of the class that contains it or it should be named __construct().


3) How would you prevent a method from being accessed except from within the current class and child classes?

  • Answer: You can limit availability of a method to the current class and child classes by using the protected keyword.


protected function dontTouchMe() {
    // no access outside current class and children
}

4) What should you add to a class definition if you want to make it inherit functionality from another class?

  • Answer: For a class to inherit from another, it must be declared with the extends keyword and the name of the class from which you want to inherit.


**************** Forms ****************

1) Which $_SERVER array element could you use to determinee the IP address of a user?

  • Answer: The $_SERVER[‘REMOTE_ADDR’] element should store the user’s IP address;


2) Which predefined variable could you use to find out about the browser that called your script?

  • Answer: Browser type and version, as well as the user’s operating ysstem, are usually stored in an element called ‘HTTP_USER_AGENT’ in the $_SERVER array.


3) What should you name your form fields if you want to find an array in the element $_REQUEST[‘form_array’]?

  • Answer: Creating multiple fields with the name form_array[] creates a populated array in $_REQUEST[‘form_array’] when the form is submitted.


4) Which superglobal associative array contains all values submitted as part of a GET request and which superglobal array $_POST
contains all values submitted as part of a POST request?

  • Answer: The superglobal array $_GET  contains all values submitted as part of a GET request. The superglobal array $_POST contains all values submitted as part of a POST request.


5) What function would you use to redirect the browser to a new page? What string would you pass it?

  • Answer: header(“Location: anotherPage.html”);


6) How can you limit the size of a file that a user can submit via a particular upload form?

  • Answer: When creating upload forms in PHP, you can include a hidden field called MAX_FILE_SIZE: <input type=”hidden” name=”MAX_FILE_SIZE” value=”51200” />


7) How can you set a limit on the size of upload files for all forms and scripts?

  • Answer: The php.ini option upload_max_filesize determines the maximum size of an upload that any script will accept.


**************** Working with files ****************



1) Will the include() statement slow down my scripts?

  • Answer: Because an include file must be opened and parsed by the engine, it will add some overhead. The benefits of reusable code libraries often outweigh the relatively low performance overhead, however.


2) Should I always end script execution if a file cannot be opened for writing or reading?

  • Answer: You should always allow for this possibility. If your script absolutely depends on the file with which you want to work, you might want to use the die() function, writing an informative error message to the browser. In less critical situations, you still need to allow for the failure, perhaps adding it to a log file.


3) Which function would you use to find out whether a file is present on your file system?

  • Answer: You can test for the existence of a file with the file_exists() function.


4) How would you determine the size of a file?

  • Answer: The filesize() function returns a file’s size in bytes.


5) Which function would you use to open a file for reading and writing?

  • Answer: The fopen() function opens a file. It accepts the path to a file and a character representing the mode. It returns a resource.


6) Which function would you use to read a line of data from a file?

  • Answer: The fgets() function reads data up to the buffer size you pass it, the end off linee, or the end of the document, whichever comes first.


7) How can you tell when you have reached the end of file?

  • Answer: The feof() function returns true when the file resource it is passed has reached the end of the file.


8) Which function you use to write a line of data to a file?

  • Answer: You can write data to a file with fputs() function.


9) How would you open a directory for reading?

  • Answer: The opendir() function enables you to open a directory for reading.


10) Which function would you use to read the name of a directory item after you have opened a directory for reading?

  • Answer: The readdir() function returns the name of a directory item from an opened directory.