Kesong Xie
Kesong Xie

Reputation: 1396

PHP block is ignored when using file_get_contents PHP

I was trying to use file_get_contents to load a phtml file and store in variable, but it seems that the php blocks are ignored

<?php  include_once '../php_inc/core.inc.php'; ?>
<div class="dialog-header">
    <div class="dialog-header-inner">
        <div class="inline-blk left-align dialog-header-left-item"><div class="bar-title vertical-center">Guitar</div></div>
        <div class="inline-blk right-align dialog-header-right-item pointer">
        <div class="bar-title dialog-header-bar-option inline-blk"><div class="vertical-center" >Post</div></div>
        <div class="bar-title dialog-header-bar-option inline-blk"><div class="vertical-center" ><img src="<?php  echo IMGDIR ?>menu_navi_icon.png" width="20" height="13"></div></div>
        </div>
    </div>
</div>

When I do something like echo file_get_contents(myfile.phtml), when I inspect the element in the chrome, it automatically comment my php block

<!--?php  include_once '../php_inc/core.inc.php'; ?-->
<div class="dialog-header">
    <div class="dialog-header-inner">
        <div class="inline-blk left-align dialog-header-left-item"><div class="bar-title vertical-center">Guitar</div></div>
        <div class="inline-blk right-align dialog-header-right-item pointer">
        <div class="bar-title dialog-header-bar-option inline-blk"><div class="vertical-center">Post</div></div>
        <div class="bar-title dialog-header-bar-option inline-blk"><div class="vertical-center"><img src="<?php  echo IMGDIR ?>menu_navi_icon.png" width="20" height="13"></div></div>
        </div>
    </div>
</div>

and the IMGDIR constant didn't get echoed as well.I know I need to use either GET OR POST to pass custom data. However, I just need to basic set up in the core.inc.php like some constant variables,etc.

Upvotes: 0

Views: 434

Answers (1)

axiac
axiac

Reputation: 72256

The function file_get_contents() reads the content of a file into a variable. It does not interpret the content of the file in any way.

You should include the file instead, if it contains PHP code and you want that PHP code to be interpreted.

Another option, when you want to read the file, execute the PHP code it contents but do not send the content it generates (HTML code outside the PHP blocks, echo(), print() etc) immediately to the output but keep it to be used later (or several times) is to use output buffering:

// Start output buffering; it redirects any generated content to a memory buffer
ob_start();
// Include the desired file; this executes the PHP code it contains
// but because of the output buffering, the HTML code is not displayed
// here but buffered
include 'myfile.phtml';
// Get the content of the buffer, clear the buffer, end the buffering
$text = ob_get_clean();
//
// ... more code and/or HTML follows
//
// When you need the content of 'myfile.phtml' you just:
echo($text);
//
// ... more code and/or HTML follows
//
// If you need to display the content of 'myfile.phtml' again you just:
echo($mytext);

Upvotes: 2

Related Questions