php include statement in Heredoc string

Discussion in 'PHP' started by krishmk, May 3, 2009.

  1. #1
    I am trying to use include statement inside a heredoc string.
    But its outputting the code to the browser.
    I have tried using
    <?php include ('header.tpl'); ?>
    PHP:
    and
    include ('header.tpl');
    PHP:
    but it doesnt seems to work.
    Would appreciate your help.
     
    krishmk, May 3, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    It's outputting the content of header.tpl to the browser correct?

    This would be because your server doesn't treat .tpl files as php. You need to add this file type to your httpd.conf file (assuming apache) so that the server treats it as php.

    Find the line that looks like this...

    AddType application/x-httpd-php .php

    add tpl to the end of it:

    AddType application/x-httpd-php .php .tpl
     
    jestep, May 4, 2009 IP
  3. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #3
    No that isn't the problem
    The code itself shows up in the browser (like below).
    <?php include ('header.tpl'); ?>

    I am able to include .tpl files with no problem except while using inside the HEREDOC string.
     
    krishmk, May 4, 2009 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    Only variables are parsed inside a heredoc, no any functions are parsed and called.
    Instead of this you can initialize some variable with "include" contents and insert this var into heredoc string:

    
    <?
    ob_start();
    include 'include.php';
    $include = ob_get_contents();
    ob_end_clean();
    
    $s = <<<EOD
    Contents of include: $include
    EOD;
    
    echo $s;
    ?>
    
    PHP:
     
    wmtips, May 4, 2009 IP
    krishmk likes this.