PHP Strings

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Php123php
P
Php123php
Community Contributor
Quizzes Created: 1 | Total Attempts: 786
Questions: 13 | Attempts: 786

SettingsSettingsSettings
PHP Quizzes & Trivia

This quiz is meant for self-study to aid in learning/memorizing the string functions in PHP.


Questions and Answers
  • 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);

    • A.

      Qwerghzi on off on

    • B.

      Qwerghzo on off on

    • C.

      Qwerghzi on on on

    • D.

      Qwerghzi off off off

    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'

    Rate this question:

  • 2. 

    What is the output of the following code snippet? echo stristr('pHPzphpZpHPZfoo', 'Z');

    • A.

      ZphpZPHPZfoo

    • B.

      ZPHPZfoo

    • C.

      Zphpzphpzfoo

    • D.

      PhpZPHPZfoo

    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

    Rate this question:

  • 3. 

    What is the output of the following script? echo strlen('foo' . chr(0) . '1'), ', ', strlen(0x00);

    • A.

      5, 1

    • B.

      3, 0

    • C.

      3, 4

    • D.

      5, 0

    • E.

      4, 0

    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

    Rate this question:

  • 4. 

    What is the function of the ucfirst and lcfirst functions?

    • A.

      To make the string's first character uppercase or lowercase, respectively.

    • B.

      To make the string's first word uppercase or lowercase, respectively.

    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.

    Rate this question:

  • 5. 

    What is the output of the following? echo strcmp('first', 'second'), ", "; echo strcmp('44', '054'), ", "; echo strcmp('0x80', 'a'), ", "; echo strcmp('a', 'A');

    • A.

      -1, 1, -1, 1

    • B.

      -1, 0, -1, 1

    • C.

      1, 0, 1, -1

    • D.

      -1, -1, -1, -1

    • E.

      -1, -1, 1, -1

    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

    Rate this question:

  • 6. 

    What is the output of: echo strspn('1800-555-5555789', '0123456789');

    • A.

      1

    • B.

      7

    • C.

      4

    • D.

      0

    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

    Rate this question:

  • 7. 

    What is the output of: echo strcspn('1-800-555-5555', '0');

    • A.

      2

    • B.

      3

    • C.

      5

    • D.

      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.

    Rate this question:

  • 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');

    • A.

      1, 1, 1, -1, 1

    • B.

      0, 0, -1, -1, 1

    • C.

      1, 1, -23, 1, -1

    • D.

      -1, 0, -1, 1, -1

    • E.

      0, 0, -23, -1, 1

    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"

    Rate this question:

  • 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);

    • A.

      Cdef, ef, de, , cde

    • B.

      Ef, ef, de, e, cde

    • C.

      Ef, ef, de, , cdef

    • D.

      Ef, ef, de, , cde

    • E.

      Abcd, de, de, , cde

    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

    Rate this question:

  • 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');

    • A.

      2, 1, 0, WARNING, 1

    • B.

      2, 1, 0, , 1

    • C.

      2, 1, 0, WARNING, 2

    • D.

      2, 1, 0, , 2

    • E.

      2, 1, 1, WARNING, 1

    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

    Rate this question:

  • 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");

    • A.

      [email protected] 0 values replaced. You should eat pizza, beer, and ice cream every day. Hll Wrld f PHP

    • B.

      Email [anti-spam] example.com 1 values replaced. You should eat pizza, beer, and ice cream every day. Hll Wrld f PHP

    • C.

      Email [anti-spam] example.com 1 values replaced. You should eat pizza, beer, and ice cream every day. Hllo World of PHP

    • D.

      Email [anti-spam] example.com 1 values replaced. You should eat ice cream, beer, and pizza every day. Hll Wrld f 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

    Rate this question:

  • 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"); }

    • A.

      This is an example string

    • B.

      This is an example string

    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

    Rate this question:

  • 13. 

    What is the output of the following? $haystack = "Hello World!'; $needle = 'lo'; echo strrchr($haystack, $needle);

    • A.

      3

    • B.

      4

    • C.

      Llo World!

    • D.

      Ld!

    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)

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jun 21, 2008
    Quiz Created by
    Php123php
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.