-1

What is causing this error?

Fatal error: Only variables can be passed by reference in /var/www/application /lib/testing/imageMaker/imageMaker.php on line 24

$x=str_replace ($s1,'',$s2);
$y=str_replace ($s1,'',$s2, 1 ); //Line 24
user1032531
  • 24,767
  • 68
  • 217
  • 387

1 Answers1

2

As described here: PHP Manual: str_replace

count

If passed, this will be set to the number of replacements performed.

You cannot pass the literals and rather pass the reference:

$x=str_replace ($s1,'',$s2);
$y=str_replace ($s1,'',$s2, $count);
echo $count;
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • I haven't used PHP in a while, but your answer does pass `$count` by reference either. You need to use `str_replace($s1, '', $s2, &$count);` to pass a reference to the `$count` variable, rather than the literal value of `$count`. – Spencer D Sep 25 '15 at 17:39
  • @SpencerDoak Ahh, good catch. I was way over my head for a moment ;) – DirtyBit Sep 25 '15 at 17:42
  • 2
    Spencer is mistaken. Despite the name, you do not define the reference when passing the variable. Doing so will generate a fatal error in PHP 5.4, and a warning in 5.3. You should change this back to `$count`. Read the note near the top of this page: http://php.net/manual/en/language.references.pass.php – rjdown Sep 25 '15 at 18:47
  • @rjdown So its safe to write it the way I had written before? – DirtyBit Sep 25 '15 at 18:59
  • 3
    Yes, it is the only way. – rjdown Sep 25 '15 at 19:04