1.
What is the output of the following code snippet?
$str = 'asdfghyo off on off';
$replace_pairs = array('a' => 'q', 's' => 'w', 'd' => 'e', 'f' => 'r', 'y' => 'z', 'o' => 'i', 'off' => 'on', 'on' => 'off');
echo strtr($str, $replace_pairs);
Correct Answer
A. Qwerghzi on off on
Explanation
Because strtr() ALWAYS looks for the longest possible match first, the 'o' => 'i' translation is not disrupted by the keys in 'off' => 'on' and 'on' => 'off' both starting with the character 'o'
Because strtr() will *NOT* try to replace stuff it has already worked on, the 'off' => 'on' translations don't translate the latter part of $str into 'on on on'
2.
What is the output of the following code snippet?
echo stristr('PHPzphpZPHPZfoo', 'Z');
Correct Answer
A. ZpHpZpHPZfoo
Explanation
stristr returns $haystack (parameter 1) from $needle onwards ($parameter 2) in a case insensitive fashion
strstr($haystack, $needle) is the case sensitive version of the function
3.
What is the output of the following script?
echo strlen('foo' . chr(0) . '1'), ', ', strlen(0x00);
Correct Answer
D. 5, 0
Explanation
0x00 (the number) when converted to a string has no length
Unlike in C, a null character won't terminate the string
Therefore D is correct
4.
What is the function of the ucfirst and lcfirst functions?
Correct Answer
A. To make the string's first character uppercase or lowercase, respectively.
Explanation
Not many other options I could throw in there ;)
This one is just for review I suppose.
5.
What is the output of the following?
echo strcmp('first', 'second'), ", ";
echo strcmp('44', '054'), ", ";
echo strcmp('0x80', 'a'), ", ";
echo strcmp('a', 'A');
Correct Answer
B. -1, 0, -1, 1
Explanation
strcmp Returns < 0 if str1 is less than str2 ; > 0 if str1 is greater than str2 , and 0 if they are equal.
//Order of precedence is: z > a, z > A, Z > 1 > 9 > 0
6.
What is the output of:
echo strspn('1800-555-5555789', '0123456789');
Correct Answer
A. 1
Explanation
strspn is a pretty FUCKING RETARDED function and only checks the INITIAL SEGMENT of parameter 1 against all characters in parameter 2
If it was 1-800-555-whatever as the first parameter, then it would only return 1, because the INITIAL SEGMENT (containing characters from JUST parameter 2) is 1 character long
7.
What is the output of:
echo strcspn('1-800-555-5555', '0');
Correct Answer
B. 3
Explanation
B) is correct because strcspn returns the INITIAL SEGMENT of param1 that does NOT contain characters from param2
params 3 and 4 are start and length, respectively, which you can change the output of the function with.
8.
What is the output of the following?
echo strncasecmp('hello world', 'HelloWorld', 5), ', ';
echo strcmp(44, 054), ', ';
echo strcasecmp('xyz', 'ABC'), ', ';
echo strncmp('0', 'Z', 1), ', ';
echo strcmp('bb', 'aaaa');
Correct Answer
E. 0, 0, -23, -1, 1
Explanation
E) is correct because for the first one, strncasecmp compares str1 to str2 in a case-insensitive fashion, for the first 5 characters
For the second one, 054 (octal 44) is converted to decimal first, and then 44 == 44
For the third one, strcasecmp, xyz is converted to uppercase or ABC is converted to lowercase, and a is 23 positions away from x in ASCII
For the fourth one, 0 is compared to Z, for the first 1 characters, letters (even uppercase ones) take precedence over numbers
Finally, for the fourth one. Even though bb is shorter than aaaa, b is higher than a, so str1 is "greater"
9.
What is the output of the following?
echo substr("abcdef", -2), ', ';
echo substr("abcdef", 4), ', ';
echo substr("abcdef", -3, -1), ', ';
echo substr("abcdef", 4, -2), ', ';
echo substr("abcdef", 2, -1);
Correct Answer
D. Ef, ef, de, , cde
Explanation
string substr ( string $string , int $start [, int $length ] )
negative start starting $start positions from the end of the string
negative length means reading in characters $length positions before the end of the string
10.
What is the output of the following?
$text = 'This is a test';
echo substr_count($text, 'is'), ', '
echo substr_count($text, 'is', 3), ', ';
echo substr_count($text, 'is', 3, 3), ', ';
echo substr_count($text, 'is', 5, 10), ', ';
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
Correct Answer
C. 2, 1, 0, WARNING, 2
Explanation
C is correct
In the first one, there's 2 counts of $needle in $haystack
In the second one, by setting the offset to 3, there's now only one $needle in $haystack
In the third one, by setting the offset to 3 and length to 3, there's now only one $needle in $haystack (would be 's i')
In the fourth one, the offset + length exceed the length of the string, throwing a WARNING
In the fifth one, overlapping $needle's are NOT counted
11.
What is the output of the following?
echo str_replace('@', ' [anti-spam] ', '[email protected]', $count);
echo $count . ' values found.';
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
echo str_replace($healthy, $yummy, $phrase);
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
echo str_replace($vowels, "", "Hello World of PHP");
Correct Answer
B. Email [anti-spam] example.com
1 values replaced.
You should eat pizza, beer, and ice cream every day.
Hll Wrld f pHP
Explanation
B) is correct
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
@ is replaced with [anti-spam]
The fourth (optional) parameter $count (passed by reference) tells you how many values were counted
If both $search and $replace are passed in as arrays, each corresponding value is replaced
In the last one, all vowels are replaced with "" or an empty string
12.
What is the output of:
$string = "This is\tan example\nstring";
$tok = strtok($string, " \n\t");
while ($tok !== false)
{
echo "Word=$tok[br /]"; //pretend like [br /] is a real line break
$tok = strtok(" \n\t");
}
Correct Answer
B. This
is
an
example
string
Explanation
In the token " \n\t" you'll notice there's a space, which is a valid delimiter, as such, B is correct, being split up across spaces as well as line breaks and tabs
13.
What is the output of the following?
$haystack = "Hello World!';
$needle = 'lo';
echo strrchr($haystack, $needle);
Correct Answer
D. Ld!
Explanation
D) is correct because strrchr returns the part of the string which contains the last occurance of $needle in $haystack, up until the end of $haystack
Also, if $needle contains more than one characters, only the first one is used (unlike strstr)