Bird
0
0

Which of the following is the correct syntax to replace 'cat' with 'dog' in the string $text using PHP?

easy📝 Syntax Q12 of 15
PHP - String Functions
Which 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);
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Validate each option

    str_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.
  3. Final Answer:

    str_replace('cat', 'dog', $text); -> Option D
  4. Quick Check:

    str_replace(search, replace, subject) [OK]
Quick Trick: Remember parameter order: search, replace, then subject [OK]
Common Mistakes:
  • Swapping search and replace
  • Using wrong function name
  • Incorrect parameter order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes