Enhance your PHP string manipulation skills with this set of 30 multiple-choice questions covering crucial topics like string functions, regular expressions, and string formatting. Perfect for developers looking to sharpen their knowledge of PHP string handling and regular expression techniques.
What will strlen("Hello") return? A) 4 B) 5 C) 6 D) “Hello”
What is the result of substr("Hello World", 6, 5)? A) World B) World! C) Hello D) rld
What does the strpos("PHP is fun", "fun") function return? A) 4 B) 7 C) 9 D) false
Which function is used to remove whitespace from the beginning and end of a string in PHP? A) trim() B) str_replace() C) ltrim() D) rtrim()
What will strrev("abc") return? A) “abc” B) “cba” C) “ab” D) “abc!”
Which function is used to find the length of a string in PHP? A) strlen() B) count() C) size() D) length()
What does strtoupper("hello") return? A) hello B) HELLO C) hEllO D) Hello
Which function is used to find a substring in a string in PHP? A) strstr() B) substr() C) strpos() D) search()
What will substr("abcdefgh", 3, 4) output? A) abcd B) defg C) efgh D) abc
How do you replace all occurrences of a substring in a string in PHP? A) substr() B) replace() C) str_replace() D) str_erase()
Topic 2: Regular Expressions in PHP (10 Questions)
Which function is used to perform a regular expression match in PHP? A) preg_match() B) reg_match() C) preg_replace() D) match_regex()
What is the result of preg_match("/hello/", "Hello world")? A) 1 B) 0 C) true D) false
Which function is used to replace a pattern in a string using regular expressions? A) preg_match() B) preg_replace() C) replace() D) regex_replace()
What will the regular expression /\d+/ match in the string “abc 123 xyz”? A) abc B) 123 C) xyz D) 123 xyz
What does the regular expression ^a match? A) The letter “a” anywhere in the string B) The letter “a” at the end of the string C) The letter “a” at the start of the string D) Any string containing “a”
What will preg_match("/\d{3}/", "abc123xyz") return? A) 123 B) true C) false D) abc
How do you specify the case-insensitive flag in PHP regular expressions? A) /i B) /s C) /m D) /c
Which function checks if a regular expression matches a given string? A) preg_match() B) preg_test() C) regex_test() D) match()
What does preg_replace("/\s+/", "-", "hello world") return? A) hello world B) hello-world C) hello-world- D) hello—world
Which of the following represents a regular expression for matching a number in PHP? A) \d B) \D C) \w D) \b
Topic 3: String Formatting (10 Questions)
What will the sprintf("I have %d apples", 5) output? A) I have 5 apples B) I have %d apples C) I have 5 apple D) 5 apples
Which function is used to output formatted strings in PHP? A) printf() B) format() C) echo_format() D) print_format()
What does printf("Pi is approximately %.2f", 3.14159) display? A) 3.14159 B) 3.14 C) 3.1 D) 3.1416
Which placeholder is used for an integer in sprintf()? A) %d B) %s C) %f D) %i
How would you format a floating-point number with two decimal places using printf()? A) %.2f B) %f.2 C) %d.2 D) %.2d
What is the result of the following code? echo sprintf("The number is %02d", 5); A) 05 B) 5 C) 005 D) 05.00
Which of these functions is used to return a formatted string without printing it? A) echo() B) print() C) sprintf() D) printf()
What will sprintf("The temperature is %.1f°C", 36.6) output? A) The temperature is 36.60°C B) The temperature is 36.6°C C) The temperature is 36°C D) The temperature is 36.66°C
How do you specify a minimum width for a string in sprintf()? A) %5s B) %s5 C) 5s D) s5
What will printf("Name: %-10s Age: %d", "John", 25) output? A) Name: John Age: 25 B) Name: John Age: 25 C) Name: John Age: 25 D) Name: John Age: 25