PHP - String FunctionsYou want to safely replace all spaces in a string with underscores in PHP. Which code snippet correctly does this?A$new = str_replace(' ', '_', $original);B$new = substr_replace($original, '_', ' ', -1);C$new = strtoupper(str_replace('_', ' ', $original));D$new = trim($original, ' ');Check Answer
Step-by-Step SolutionSolution:Step 1: Understand the goalWe want to replace all spaces (' ') with underscores ('_') in the string.Step 2: Identify the correct functionstr_replace(' ', '_', $original) replaces all spaces with underscores safely.Step 3: Check other optionssubstr_replace is for replacing part by position, not all spaces; strtoupper changes case; trim removes spaces only at ends.Final Answer:$new = str_replace(' ', '_', $original); -> Option AQuick Check:str_replace() replaces all occurrences [OK]Quick Trick: Use str_replace() to swap all spaces with underscores [OK]Common Mistakes:Using substr_replace which replaces by position, not all spacesUsing strtoupper which changes case, not charactersUsing trim which only removes spaces at string ends
Master "String Functions" in PHP9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PHP Quizzes Array Functions - Array key and value extraction - Quiz 12easy Classes and Objects - Static properties and methods - Quiz 14medium Error and Exception Handling - Why error handling matters - Quiz 2easy Interfaces and Traits - Interface constants - Quiz 6medium Interfaces and Traits - Interface constants - Quiz 1easy Interfaces and Traits - Interface declaration and implementation - Quiz 12easy Sessions and Cookies - Setting and reading cookies - Quiz 14medium String Functions - String split and explode - Quiz 5medium String Functions - String comparison functions - Quiz 9hard Superglobals and Web Context - Input validation and sanitization - Quiz 8hard