0

Given the following example:

public function replaceMyText($search, $replace, &$content)
{
    $newContent = str_replace($search, $replace, $content, $count = 1)
    $content = $newContent;
}

Can this cause a Warning that only variables can be passed by reference? If so, I can't fully understand why.

Should I assigned the $content to another variable before passing it to the str_replace function?

LostCoder
  • 51
  • 1
  • 1
  • 5
  • Did you try it? Did it generate a warning? – AbraCadaver Aug 30 '18 at 15:43
  • Yes, it does... and I don't understand why. I tried the code, not the solution I proposed since I didn't understand the "why" – LostCoder Aug 30 '18 at 15:44
  • 2
    How are you calling this? Are you just passing a raw string as the third argument? – iainn Aug 30 '18 at 15:44
  • The error is coming from `$count = 1`. See http://php.net/manual/en/function.str-replace.php – AbraCadaver Aug 30 '18 at 15:46
  • Now I'm confused again. You say you're getting a warning, but this should either throw a fatal error if you're calling `replaceMyText` with a raw string, or a notice if it's the call to `str_replace` that's the problem. I'm not sure how you'd see a warning. Which are you seeing? – iainn Aug 30 '18 at 15:48
  • @iainn It's a `notice`, not a warning. Haven't paid enough attention to Rollbar. (Sorry). And yes, I am calling `replaceMyText` where the 3rd param is a string. – LostCoder Aug 30 '18 at 15:54
  • Ok, that makes sense. If you're not using the `$count` parameter then just remove it from the function call entirely. – iainn Aug 30 '18 at 15:55

2 Answers2

1
<?php
function replaceMyText($search, $replace, &$content)
{
    $newContent = str_replace($search, $replace, $content, $count = 1);
    $content = $newContent;
}

replaceMyText("123", "456", "123456");

using this function without variable will get a fatal error

Fatal error: Only variables can be passed by reference in /usercode/file.php on line 8

because

No other expressions should be passed by reference, as the result is undefined.

from http://php.net/manual/en/language.references.pass.php

you just can using by this

$a = "123456";
replaceMyText("123", "456", $a);
echo $a;

Sorry for my bad english. I hope it can help you.

Zane
  • 209
  • 1
  • 8
0

EDIT

Please try this code :

function replaceMyText($search, $replace, $content)
{
    $newContent = str_replace($search, $replace, $content,$count=1);
    return $newContent;
}

$searchValue = "test";
$replaceWith = "magic trick";
$actualContent = "This is a test.";

$replaced = replaceMyText($searchValue,$replaceWith,$actualContent);

echo $replaced;
  • I did check that link, and this example http://php.net/manual/en/function.str-replace.php#108196 states exactly my solution as "working", unless I missed something. – LostCoder Aug 30 '18 at 15:56
  • @LostCoder Assigning a variable at the same time as passing it to a function did work in older versions of PHP, but was always a bad idea. I'm not sure which version was being used when that example was written, but it definitely isn't good practice. – iainn Aug 30 '18 at 16:04