Dear experts I ask for help, help add a reference to the variable in the value of the array element.

$cnt_att = 8; //общее количество попыток $att = &$cnt_att; $status[8] = array('error', "Неверный код подтверждения, осталось $att попыток", 5000, 8); $cnt_att--; echo $status[8][1]; //Неверный код подтверждения, осталось 8 попыток ?> 

Issues: 8 attempts, but should be 7, how to do?

Here is a simplified example:

 <?php $cnt_att = 8; $att = &$cnt_att; $mes = "Неверный код подтверждения, осталось $att попыток"; $cnt_att--; echo $mes; //Неверный код подтверждения, осталось 8 попыток ?> 

Doesn't work either

  • one
    you first create the status with an error, and after you subtract the variable. $cnt_att--; put this code before $mes = "Неверный код подтверждения, осталось $att попыток"; - Evgeny Nikolaev
  • @ EvgenyNikolaev, in the status of a link to the variable - Igor Salamov
  • one
    At the time of creating the status, the variable is equal to 8, and this value is substituted into the text. Do what @ YevgenyNikolaev writes. - Enikeyschik
  • @ Enikeyschik thank you, the situation is simply simplified, you need the ability to change after, how to add a variable reference to the string, something like "Неверный код подтверждения, осталось" . &$cnt_att. " попыток"; "Неверный код подтверждения, осталось" . &$cnt_att. " попыток"; - Igor Salamov
  • one
    Why does she need such an opportunity? Build a string every time before displaying and there will always be a current value in it. - Enikeyschik

1 answer 1

You can also use output buffering (if you need to change the number in the message, and for some reason you cannot implement it correctly) :

 ob_start(); $cnt_att = 8; $mess = 'Неверный код подтверждения, осталось {att} попыток'; echo $mess; $buffer = ob_get_clean(); echo str_replace('{att}', --$cnt_att, $buffer); 
  • one
    Thanks Edward, buffering is not necessary for me, but the option with the placeholder is very interesting, thank you, I will use it. - Igor Salamov