PHP Strings

13 Questions | Total Attempts: 204

SettingsSettingsSettings
Please wait...
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

  • 2. 
    What is the output of the following code snippet? echo stristr('PHPzphpZPHPZfoo', 'Z');
    • A. 

      ZphpZPHPZfoo

    • B. 

      ZPHPZfoo

    • C. 

      Zphpzphpzfoo

    • D. 

      PhpZPHPZfoo

  • 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

  • 4. 
    • A. 

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

    • B. 

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

  • 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

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

      1

    • B. 

      7

    • C. 

      4

    • D. 

      0

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

      2

    • B. 

      3

    • C. 

      5

    • D. 

      0

  • 8. 
    • 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

  • 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

  • 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

  • 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

  • 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

  • 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!