Front | Back |
Consider the following script. What line of code should be inserted in the marked location in order to display the string php when this script is executed?
|
The substr function could work for this task, but, because the only available answer that
makes use of this function extracts two characters at a time, it must be ruled out. This leaves either $alpha{$val} or $alpha{$val+1} as the only two options that can actually print the desired string. Since strings can be accessed as zero-indexed arrays (meaning that the first character in the string has an index of zero), the correct answer is D.
|
Which of the following will not combine strings $s1 and $s2 into a single string?
A. $s1 + $s2
B. "{$s1}{$s2}"
C. $s1.$s2
D. implode('', array($s1,$s2))
E. All of the above combine the strings
|
Each of the answers will produce the desired result of concatenating the string $s1 with the
string $s2, with the exception of Answer A. In PHP, the plus operator (+) does not combine two strings together as it does in other languages, such as Java or Javascript.
|
Given a variable $email containing the string [email protected], which of the following statements would extract the string example.com?
A. substr($email, strpos($email, "@"));
B. strstr($email, "@");
C. strchr($email, "@");
D. substr($email, strpos($email, "@")+1);
E. strrpos($email, "@");
|
The substr function is used to return a portion of a string, while the strpos function is good for finding a particular substring within a string. Used together, they can extract the required information. It is important to realize that former is zero-indexed while the latter is not, requiring a +1 offset to be added. That’s why Answer D is correct.
|
Given a comma-separated list of values in a string, which function from the given list can
create an array of each individual value with a single call?
A. strstr()
B. Cannot be done with a single function
C. extract()
D. explode()
E. strtok()
|
The correct answer is D. The explode function creates an array from a string breaking it up based on a specific character such as a comma. The strtok function could also be used to tokenize the string but it would require multiple calls.
|
What is the best all-purpose way of comparing two strings?
A. Using the strpos function
B. Using the == operator
C. Using strcasecmp()
D. Using strcmp()
|
Answer E is correct—strcmp() offers the safest comparison mechanism between two strings. Note that Answer C is incorrect because strcasecmp() is not an “all-purpose” function, since it performs a case-insensitive comparison.
|
Which of the following PCRE regular expressions best matches the string php|architect?
A. .*
B. ...|.........
C. \d{3}\|\d{8}
D. [az]{3}\|[az]{9}
E. [a-z][a-z][a-z]\|\w{9}
|
None of these regexes really represents the simplest way to match the requested string, but Answers A and E are the only ones capable of doing so. However, Answer A is too generic, since it will really match any string; therefore, Answer E is correct.
|
Which of the following functions can be used to determine the integrity of a string? (Choose
3)
A. md5()
B. sha1()
C. str_rot13()
D. crypt()
E. crc32()
|
The correct choices are A, B and E. Using crypt() and str_rot13() would be an inefficient way of determining whether the contents of a string have changed since the moment in which the original digest was calculated. While crc32() is weaker than the other two choices, it’s a very viable alternative in situations where a small chance of error is acceptable.
|
Which PHP function does the following script simulate on a UNIX machine?
|
The file function reads the contents of a text file inside an array, one element per line.
Therefore, Answer E is correct. If you’re wondering what this question is doing in the
chapter dedicated to strings—it’s here to remind that you that the questions in the exam are not strictly compartmentalized, just like a PHP script cannot normally be written so that the file functions are all kept separate from the string functions.
|
Which of the following functions can be used to break a string into an array based on a
specific pattern? (Choose 2)
A. preg_split()
B. ereg()
C. str_split()
D. explode()
E. chop()
|
Both the preg_split and explode functions can be used for this purpose, although under
different circumstances. ereg() is used to match strings against a regular expression pattern, while str_split() only breaks down a string based on a fixed length and chop() is simply an alias for rtrim(), which removes whitespace from the end of a string.
|
What will the following script output?
|
This question tests your knowledge of string manipulation and operator precedence. The
concatenation operator has a higher precedence than the addition operator. Therefore, PHP will interpret this expression as if it were written as ('Testing ' . 1) + (2 . '45'). When the addition takes place, the first concatenation is evaluated as the integer zero, since the string Testing 1 is not a valid number. The second concatenation is evaluated as the number 245 and, therefore, PHP outputs the result of 0 + 245, that is, 245. Therefore, Answer D is
correct.
|
What will be the output of the following script?
|
Since strings can be addressed as arrays, this script simply replaces the value of the second
characters (identified by $s[1]) with the character 2, resulting in the string 12245 being printed out. Answer B is correct.
|
Which of the strings below will be matched by the following PCRE regular expression?
(Choose 2)
/.*\*123\d/
A. ******123
B. *****_1234
C. ******1234
D. _*1234
E. _*123
|
The trick here is in understanding what the regular expression means. Reading from left to right, it indicates a string composed of zero or more arbitrary characters (.*), followed by an asterisk (\*), then by the literal 123 and, finally, by a digit. Therefore, the right answers are C and D.
|
Which of the following comparisons will return True? (Choose 2)
A. '1top' == '1'
B. 'top' == 0
C. 'top' === 0
D. 'a' == a
E. 123 == '123'
|
Answers B and E are correct. In Answer B, the string top is evaluated to integer zero when the comparison takes place and, since the == operator does not perform strict type checking, it returns True. In answer E, the same thing happens to the string 123, which is evaluated to the integer number 123, thus resulting in a successful comparison.
|
What happens if you add a string to an integer using the + operator?
A. The interpreter outputs a type mismatch error
B. The string is converted to a number and added to the integer
C. The string is discarded and the integer is preserved
D. The integer and string are concatenated together in a new string
E. The integer is discarded and the string is preserved
|
Naturally, the string is converted to a number (or zero if such conversion cannot take place)
and added to the integer using arithmetic addition. Answer B is correct.
|
Consider the following script. Assuming that can be successfully read, what will it output?
<?php
$s = file_get_contents ("");
strip_tags ($s, array ('p'));
echo count ($s);
?>
A. The length of the homepage
B. The length of the homepage stripped of all its <p> tags
C. 1
D. 0
E. The length of the homepage stripped of all its tags except for <p> tags
|
As it is often the case when looking for a bug, the intent of the script is quite irrelevant in
this question. Up until the very last line of code, in fact, its goal is to strip the homepage of all of its HTML tags, with the exception of instances of <p>. However, on the last line the script uses the count function, which does not count the number of characters in a string, but the number of elements in a variable. Since strings are scalar values, count() always returns one—and Answer C is correct
|