There is such a piece of code, everything seems to work, but there is one thing but the function file_get_contents does not want to read the code from the php file, reads it as a comment

 $header = file_get_contents('tpl/header.php'); 

This code is needed in order not to write to a template, of type include('tpl/header.php') , but simply put

 <?=$header?> 

Mostly for beauty!

    4 answers 4

    Alternatively, you can cram the entire output of header.php into the $ header variable, i.e .:

     <? $header = " <header> ... </header>"; 

    and then connect

     <?=$header?> 
    • one
      The identity of the rules is an option, but I need to enter the variable not directly, but through the connection of the file. And so thanks !!! - webkostya

    It’s terrible to do this, of course, but since you’ve decided to quit, then try:

     ob_start(); eval(file_get_contents('tpl/header.php'); $header = ob_get_clean(); //или проще ob_start(); include('tpl/header.php'); $header = ob_get_clean(); 
    • one
      And if you need to assign several variables to include, for example, $ header = file_get_contents ('tpl / header.php'); $ footer = file_get_contents ('tpl / footer.php'); I'm sorry but I'm not special in PHP - webkostya
    • 7
      stop using fucking eval, omg - Zowie
    • one
      @AlexWindHope absolutely agrees with you, in general, it is better not to use any of the constructions given in the answer. @Berserker repeat the same thing, just change the header to footer. - FLK
    • And why assign something to variables? Is it possible to simply make the necessary file in the right place? - Photon
    • one
      All this for beauty and convenience - webkostya

    Try looking at Twig or other ready-made crafts.

    • one
      Thank! But this is superfluous. Why strain the site in vain. PHP itself as a template engine. - webkostya
    • one
      @ Berserker, you are wrong. I will not explain, write too much ... - johniek_comp
    • one
      It may well be wrong. Just read about these template engines, that's why I don't speak well of them. - webkostya
    • one
      Look, maybe like it! - webkostya
    • And it’s better to write your own template engine with minimal functionality. Good experience ... - iproger

    And if so:

     <?=eval($header)?> 

    Already not beautiful?

    • one
      On such swears! - webkostya
    • What does he write? And yes, you can open header.php without errors separately ?, if so, you can do it differently without tweaking eval - iproger
    • so you do not need to echo removed. <? eval ($ header)?> - FLK
    • one
      We need to replace this file_get_contents function with some other one - webkostya
    • one
      There is one html in the header.php file, but I would like to put PHP, but not. And such a method which FLK suggests does not want to work gives an error. - webkostya