PHP - String FunctionsWhich of the following is the correct syntax to replace 'cat' with 'dog' in the string $text using PHP?Astr_replace('dog', 'cat', $text);Breplace('cat', 'dog', $text);Cstr_replace($text, 'cat', 'dog');Dstr_replace('cat', 'dog', $text);Check Answer
Step-by-Step SolutionSolution:Step 1: Check the correct parameter order of str_replace()The correct order is str_replace(search, replace, subject). So 'cat' is search, 'dog' is replace, $text is subject.Step 2: Validate each optionstr_replace('cat', 'dog', $text); matches correct order. replace('cat', 'dog', $text); uses a non-existent function. str_replace($text, 'cat', 'dog'); swaps parameters incorrectly. str_replace('dog', 'cat', $text); swaps search and replace.Final Answer:str_replace('cat', 'dog', $text); -> Option DQuick Check:str_replace(search, replace, subject) [OK]Quick Trick: Remember parameter order: search, replace, then subject [OK]Common Mistakes:Swapping search and replaceUsing wrong function nameIncorrect parameter order
Master "String Functions" in PHP9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PHP Quizzes Array Functions - Why array functions matter - Quiz 5medium Classes and Objects - Constants in classes - Quiz 3easy Classes and Objects - Why OOP is needed in PHP - Quiz 13medium Classes and Objects - Class declaration syntax - Quiz 8hard File Handling - JSON encoding and decoding - Quiz 6medium Inheritance and Polymorphism - Instanceof operator - Quiz 1easy Sessions and Cookies - Starting and using sessions - Quiz 12easy String Functions - String comparison functions - Quiz 9hard String Functions - String split and explode - Quiz 11easy Superglobals and Web Context - Form handling execution flow - Quiz 11easy