// code ... echo $sTitle; // Как тут вывести 'Привет!', который инициализируется позже вывода? // code ... $sTitle = 'Привет!'; 

Is it possible?

  • Of course nothing. It is better to voice the whole problem, because its solution may be different. - PinkTux
  • We do not know what is hidden under // code ... It may well be that everything is possible - KoVadim
  • This option is of interest, there is no complete problem, the interest of the existence of such a method. How do you pass a function through a function to an already existing variable and output it? - j-tap
  • if without // code... ? - j-tap
  • like this function Hi($sTitle) { echo $sTitle; } $sTitle = 'Привет!'; Hi($sTitle); function Hi($sTitle) { echo $sTitle; } $sTitle = 'Привет!'; Hi($sTitle); - KoVadim

2 answers 2

You can enable buffering, place anchors, and then in their place insert the necessary values

http://php.net/manual/ru/function.ob-start.php

 <?php ob_start(); echo '$sTitle'; $sTitle = 'Привет!'; $out=ob_get_clean(); echo str_replace('$sTitle',$sTitle,$out); 

    In principle, the code above can be made to work as you want (although I don’t imagine why this may be necessary and it’s better not to do so ):

     $assigned = false; myLabel: echo $sTitle; $sTitle = 'Привет!'; if (!$assigned) { $assigned = true; goto myLabel; } 

    In this case, obviously, we still see the warning Undefined variable: sTitle (with the appropriate level of error_reporting ).

    You can still dodge through self-include:

     if (!isset($included)) { $included = true; include __FILE__; } echo $sTitle; $sTitle = 'Привет!'; 
    • Wow, interesting solution, thanks! I don’t need anything yet, they argued that it’s not possible with php, I was sure that it should be possible) I’m connected with php only with rare edits in bitrix templates) thanks for the answer! - j-tap