-2
Follows the function:
$conteudo = $textodobanco;
function addBanner($i) {
static $conta1 = 0;
$retorno = $i[0];
if (++$conta1 == 5) {
$retorno .= '<b>BANNER 1</b>';
}
return $retorno;
}
$txt = preg_replace_callback('#(<br />)#', 'addBanner', $conteudo);
The above function works perfectly, however, I need that instead of using the number 5 expressed in the comparison, that it be replaced by a variable, so as to dynamize the system I am developing.
However, all attempts like below on replacing the number 5 by a variable did not work, ie the function does not return any result.
Substitution by variable, however, without success
function addBanner($i) {
static $conta1 = 0;
$retorno = $i[0];
if (++$conta1 == $numerovar) {
$retorno .= '<b>BANNER 1</b>';
}
return $retorno;
}
@rray as shown in the first example, the number 5 is expressed. I need to put a variable in its place. $i is something else.
– Gladison
In the function enter two parameters, after the
$i
can call a$limite
can set a default value for it. ex:function addBanner($i, $limite=3)
– rray
@Gladison the variable you will use to perform the comparison is engraved in the bank?? already checked what is being returned?
– Oliveira
@Oliveira Yes, it’s coming from the database and I checked the value returned, in this case, 5. However, this function does not work if I replace the number five by the variable. I don’t know why it happens.
– Gladison
The statement is unrelated to the problem. The comparison of your code is correct, what is not is the lack of a passage or creation of the variable (you compare with
$numerovar
but does not pass this variable to the function in any way). It would be important to [Edit] reduce the code to a [mcve] if you want more details. The answer below explains the ideal solutions (regardless of whether the value comes from DB or not, the path is the same). - Essential to study the PHP manual for scope of variables.– Bacco