As liquorvicar asked, we need some clarification on whether you want this script to output the text "<?php include('header.htm') ?>" or if you actually want to load the header.htm file and output its data.

Also, it might be easier for you to use the HEREDOC syntax:

$output = <<<END_OF_STRING
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN
                    http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<title>TA Generic Procedures - {$title}</title>

etc.

END_OF_STRING;

    I'm guessing, what you try to achieve is to put what would be normally displayed in browser with include in some varible?
    If so then output buffering is what you need:

    $output = "whatever at the beginning";
    
    ob_start(); //start output buffering
    include('../header.html'); // this isn't sent to the browser, it's buffered instead
    $output.=ob_get_contents(); //add what's in the buffer to the varible
    ob_end_clean(); //turn off output buffering and clean the buffer (without sending to browser)
    
    $output.="whatever should be next";
    

      yes I want the string to output <?php include('header.htm') ?>

      How do I do this? Thanks.

      Tried HEREDOC...

      
      <?php
      
      $output = <<<PAGE_CONTENT
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
      
      <head>
      <title>TA Generic Procedures - Operations Procedures - List of Funds</title>
      
      <link rel="stylesheet" type="text/css" href="../css/screen.css" />
      
      <script language="JavaScript" src="../menu.js"></script>
      
      </head>
      
      <body>
      
      <div id="wrapper">
      
      
      <div id="header">
      
      	  <?php
      
      		  include('../header.htm');
      
      		?>
      
      	</div> <!-- end Header -->
      
      
      <div id="content">
      
      	  <h1>List of Funds &amp; Relevant Dealing Cut-Off Points</h1>
      
      		<?php
      
      		  // load image
      			include('../display_img.php');
      
      		  // connect to db
      			include('../db_connect.php');
      
      			$query = "SELECT list_of_funds, last_updated ".
      						 	 "FROM operations_procedures";
      			$result = mssql_query($query) or die('Select Error');
      
      			$row = mssql_fetch_array($result);
      
      			echo '<p class="edit_date">'.$row['last_updated'].'</p>';
      
      			echo $row['list_of_funds'];
      
      		?> 
      
      
      </div> <!-- end Content -->
      
      
      	<div id="sidecol">
      
      	  <?php
      		  include('../menu.htm');
      		?>
      
      	</div> <!-- end Side Column -->
      
      
      </div> <!-- end Wrapper -->
      
      </body>
      </html>
      
      PAGE_CONTENT;
      
      

        I still don't understand what you try to achieve. But let's say you have

        <?php
        //header.php
        echo "This is the first header line<br>";
        echo "This is the second header line<br>";
        echo "This is the third header line<br>";
        ?>
        

        Thera are three possibilities what you can do:

        1. echo string only

        <?php
        $output =  htmlspecialchars("<?php include('header.php');?>");
        
        echo $output;
        ?>
        

        This will show that $output is

        <?php include('header.php');?>

        2. echo the content of the file

        <?php
        $output =  nl2br(htmlspecialchars(file_get_contents('header.php')));
        
        echo $output;
        ?>
        

        This will show that $output is

        <?php
        //header.php
        echo "This is the first header line<br>";
        echo "This is the second header line<br>";
        echo "This is the third header line<br>";
        ?>
        

        3. echo the result of executing header.php

        <?php
        ob_start();
        include('header.php');
        $output = ob_get_contents();
        ob_end_clean();
        
        echo $output;
        ?>
        
        This is the first header line<br>
        This is the second header line<br>
        This is the third header line<br>
        

        There isn't much more you can do with that file and the varible 😉

          1/ I have a blank page template and want to make a copy of it as below...

          			 $file = 'template.php'; //TEMPLATE
          			 $newfile = $title.'.php'; // FILE COPY
          
          		 $filedir = '../'.$folder.'/'.$newfile; // FILE DIRECTORY
          
          		 // COPY OVER FILE TEMPLATE
          		 if (!copy($file, $filedir)) {
          	       //echo "failed to copy $file...\n";
          		 }
          		 else
          		 {
           		       //echo 'File Created.';
          		 }
          

          2/ Then I want to write $output to this new page...

          			 // OPEN COPIED FILE
          			 $fp = fopen($filedir, "w");
          
          		 fwrite($fp, $output); // WRITE TO FILE
          
          		 fclose($fp); // CLOSE FILE
          

          The new page should contain this code...

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
          
          <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
          
          <head>
          <title>TA Generic Procedures - Impress Procedures</title>
          
          <link rel="stylesheet" type="text/css" href="../css/screen.css" />
          
          <script language="JavaScript" src="../menu.js"></script>
          
          </head>
          
          <body>
          
          <div id="wrapper">
          
          
          <div id="header">
          
          	  <?php
          
          		  include('../header.htm');
          
          		?>
          
          	</div> <!-- end Header -->
          
          
          <div id="content">
          
          	  <h1>Impress Procedures</h1>
          
          		<?php
          
          		  // load image
          			include('../display_img.php');
          
          		  // connect to db
          			include('../db_connect.php');
          
          			$query = "SELECT impress, last_updated ".
          						 	 "FROM impress_procedures";
          			$result = mssql_query($query) or die('Select Error');
          			//echo 'TR:'.$num_results = mssql_num_rows($result);
          
          			$row = mssql_fetch_array($result);
          
          			echo '<p class="edit_date">Last Updated: '.$row['last_updated'].'</p>';
          
          			echo stripslashes($row['impress']);
          
          		?> 
          
          
          </div> <!-- end Content -->
          
          
          	<div id="sidecol">
          
          	  <?php
          		  include('../menu.htm');
          		?>
          
          	</div> <!-- end Side Column -->
          
          
          </div> <!-- end Wrapper -->
          
          </body>
          </html>
          
          

          so how do I assign this to $output????

          sorry for not clearly explaining it.

            Now I get it. 🙂

            HEREDOC is a way to go. Just change all your $ there to \$ so it doesn't treat varibles as varibles.

            $output = <<<PAGE_CONTENT
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            
            <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
            
            <head>
            <title>TA Generic Procedures - Operations Procedures - List of Funds</title>
            
            <link rel="stylesheet" type="text/css" href="../css/screen.css" />
            
            <script language="JavaScript" src="../menu.js"></script>
            
            </head>
            
            <body>
            
            <div id="wrapper">
            
            
            <div id="header">
            
                  <?php
            
                      include('../header.htm');
            
                    ?>
            
                </div> <!-- end Header -->
            
            
            <div id="content">
            
                  <h1>List of Funds &amp; Relevant Dealing Cut-Off Points</h1>
            
                    <?php
            
                      // load image
                        include('../display_img.php');
            
                      // connect to db
                        include('../db_connect.php');
            
                        \$query = "SELECT list_of_funds, last_updated ".
                                          "FROM operations_procedures";
                        \$result = mssql_query(\$query) or die('Select Error');
            
                        \$row = mssql_fetch_array(\$result);
            
                        echo '<p class="edit_date">'.\$row['last_updated'].'</p>';
            
                        echo \$row['list_of_funds'];
            
                    ?>
            
            
            </div> <!-- end Content -->
            
            
                <div id="sidecol">
            
                  <?php
                      include('../menu.htm');
                    ?>
            
                </div> <!-- end Side Column -->
            
            
            </div> <!-- end Wrapper -->
            
            </body>
            </html>
            
            PAGE_CONTENT;
            

              thanks!

              How do I deal with the semicolon ; here... include('../header.htm');

                Make sure you have no trailing spaces at the end of your heredoc.

                PAGE_CONTENT;
                

                When I copied and pasted it there was a space at the end of this line which was causing a problem.

                  still have same issue. Page is displayed as blank so there must be a syntax error.

                  <?php
                  
                  //list($title1, $title2) = split('[/-._]', $title); // SET HEADING
                  
                  
                  $output = <<<PAGE_CONTENT
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                  
                  <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
                  
                  <head>
                  <title>TA Generic Procedures - Operations Procedures - List of Funds</title>
                  
                  <link rel="stylesheet" type="text/css" href="../css/screen.css" />
                  
                  <script language="JavaScript" src="../menu.js"></script>
                  
                  </head>
                  
                  <body>
                  
                  <div id="wrapper">
                  
                  
                  <div id="header">
                  
                        <?php
                  
                            include('../header.htm');
                  
                          ?>
                  
                      </div> <!-- end Header -->
                  
                  
                  <div id="content">
                  
                        <h1>List of Funds &amp; Relevant Dealing Cut-Off Points</h1>
                  
                          <?php
                  
                            // load image
                              include('../display_img.php');
                  
                            // connect to db
                              include('../db_connect.php');
                  
                              $query = "SELECT $title, last_updated ".
                                                "FROM $table";
                              $result = mssql_query($query) or die('Select Error');
                  
                              $row = mssql_fetch_array($result);
                  
                              echo '<p class="edit_date">'.$row['last_updated'].'</p>';
                  
                              echo $row['list_of_funds'];
                  
                          ?>
                  
                  
                  </div> <!-- end Content -->
                  
                  
                      <div id="sidecol">
                  
                        <?php
                            include('../menu.htm');
                          ?>
                  
                      </div> <!-- end Side Column -->
                  
                  
                  </div> <!-- end Wrapper -->
                  
                  </body>
                  </html>
                  
                  PAGE_CONTENT;
                  ?>
                  
                  

                  Then in the main page the above code is called here...

                  include('output.php');

                  Thanks!

                    Have you got display_errors and error_reporting turned on? That would tell you if you had a syntax error.

                    Otherwise, are you actually echo-ing the $output variable somewhere?

                      Get this error now...

                      Parse error: parse error, expecting T_STRING' orT_VARIABLE' or `T_NUM_STRING' in C:\Inetpub\wwwroot\transfer_agency\new_page\output.php on line 55

                      What does this mean?

                      I set display_errors = On in my php.ini file

                        Don't forget you need to escape your variables with a backslash, i.e. "\$variable" rather than "$variable".

                          Line 55 is the ?> in this section...

                                <?php
                          
                                    include('../header.htm');
                          
                                 ?>

                          No variables there and not sure what to do with the ?>

                          Any ideas? Thanks.

                            As I said, make sure you escape your dollar-signs. The next non-whitespace after line 55 is a variable with a dollar-sign.

                              Sorry, my last post was rubbish.

                              According to my IDE, line 55 is

                              echo '<p class="edit_date">'.$row['last_updated'].'</p>';
                              

                              As I've said twice before, try escaping all variables in the heredoc with a backslash, i.e. the line above becomes

                              $output = <<<PAGE_CONTENT
                              echo '<p class="edit_date">'.\$row['last_updated'].'</p>';
                              PAGE_CONTENT;
                              

                                Hi again,

                                Any ideas why this doesnt display as html...

                                <?php echo ucwords(\$title1).' '.ucwords(\$title2); ?>

                                  What do you mean, doesn't display as HTML?
                                  Are you including this in a heredoc style string assignment? Or just as a normal part of your PHP script?

                                    sorry. my brain was fried. works fine. thanks.

                                      Write a Reply...