Search code examples
phphtmlinclude

Using php include function in html within php variable


i am doing something like this

   $some_var='<!DOCTYPE html>
              <head>
              <title>questions '.$suto.'</title>
              <meta charset="utf-8" />
              <meta name="description" content="question need to'. $suto.'">
              '. include ("header.php") .'
              <body>       

There is more code below which is working that's why not posting here.

Everything works instead the include function.


Solution

  • Instead of include() you can use file_get_contents() like,

    $some_var='<!DOCTYPE html>
        <head>
                <title>questions '.$suto.'</title>
                 <meta charset="utf-8" />
                 <meta name="description" content="question need to'.$suto.'">'.file_get_contents("header.php").'
        <body>   
    

    file_get_contents() is a PHP function which is one of the preferred way to read the contents of a file into a string.

    include() will parse PHP code inside the included file. And file_get_contents() will return the content of the file. Here, you are storing the content to a string. So the file_get_contents is the right option.