Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - String Functions
What will be the output of the following PHP code?
$text = "I love apples.";
$result = str_replace("apples", "oranges", $text);
echo $result;
AI love oranges.
BI love apples.
CI love apples.oranges
DSyntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand str_replace behavior

    str_replace replaces all occurrences of the search string with the replacement string in the given text.
  2. Step 2: Apply replacement to the string

    "apples" is replaced by "oranges" in "I love apples.", resulting in "I love oranges.".
  3. Final Answer:

    I love oranges. -> Option A
  4. Quick Check:

    Replace 'apples' with 'oranges' = I love oranges. [OK]
Quick Trick: str_replace returns new string; echo the result to see changes [OK]
Common Mistakes:
  • Expecting original string unchanged
  • Not assigning result to variable
  • Syntax errors in code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes