How to compare variable value with increment operator?

Asked

Viewed 61 times

-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.

  • 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)

  • @Gladison the variable you will use to perform the comparison is engraved in the bank?? already checked what is being returned?

  • @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.

  • 3

    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.

1 answer

1

To make the comparison with a variable first you need either create it or pass it as Parameter...

Follow an example passing it as Parameter:

function addBanner($i, $numerovar)
{
   static $conta1 = 0;
   $retorno = $i[0];
   if(++$conta1 == $numerovar)
   {
      $retorno .= '<b>BANNER 1</b>';
   }
   return $retorno;
}

Follow an example Creating she:

function addBanner($i)
{
   static $conta1 = 0;
   $retorno = $i[0];
   $numerovar = 10;
   if(++$conta1 == $numerovar)
   {
      $retorno .= '<b>BANNER 1</b>';
   }
   return $retorno;
}
  • Unfortunately it didn’t work. My variable $numerovar receives a database number. I cannot put a predefined number in the function.

  • In your first example I assigned the variable $num a bank value and nothing.

  • Getting something?

  • 1

    Dear Marccus your idea is almost right, if I understand correctly, but the AP code is with preg_replace_callback, as this does not work, a suggestion would be to use an anonymous function, example: https://gist.github.com/brcontainer/f85ff8dc606840d77803b04cabc2b24e (cc @Gladison see if you can understand) (updated code in gist, missing a detail)

  • @Guilhermenascimento Perfect, it worked!!! Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.