P
P
pimanov32021-07-08 15:26:07
PHP
pimanov3, 2021-07-08 15:26:07

Why does this record produce a fatal error?

Why does this record produce a fatal error? Meanwhile, so true:
$text = str_replace("[id", "[", $text, 1);

$count = 1;
$text = str_replace("[id", "[", $text, $count);

Tell me what to do, I do not want to create a separate variable for this argument.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2021-07-08
@pimanov3

Because $count is not a limit on the number of replacements, but it records how many times the replacement worked.
Therefore, it is necessary to pass a variable there, not a number.
sandbox.onlinephpfunctions.com/code/87f0105ff5614c...

str_replace(
    array|string $search,
    array|string $replace,
    string|array $subject,
    int &$count = null
): string|array

https://www.php.net/manual/ru/function.str-replace.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question