0

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.

2
  • I am not sure that your PHP code is commented ... but I think your file is not a .php file that is why PHP code is not being executed Commented Jun 20, 2015 at 15:47
  • I don't think would be the reason, I usually use phtml extension to distinct that the file contains more html codes while with a few lines php codes embedded, but it seems the file_get_contents treat it in a different way Commented Jun 20, 2015 at 15:50

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.