I am trying to interpolate a string from an external file but I am having issues using complex curly braces. The variables do not populate as expected? If i take the code from the external template file it successfully returns a value for each variable.
index.php
function getIncludeContents($included_file_path, $post){
if ( !file_exists( __DIR__ . $file ) ) {
return 'nofile';
}
if ( is_array( $post ) ){
extract( $post );
}
ob_start();
include($included_file_path);
return ob_get_clean();
}
$navigation = getIncludeContents('layouts/test.php', $post);
var_dump($navigation);
test.php
<div id="post-{$post['id']}"></div>
this returns...
'<div id="post-${post['id']}"></div>'
when it should return...
'<div id="post-201"></div>
any ideas why? thanks
CodePudding user response:
Try this way
<div id="post-<?php echo $post['id']?>"></div>
CodePudding user response:
Since test.php
doesn't have <?php
, it's being treated as fixed text, not PHP code to execute. It needs echo
statements to echo variables, or you can use the short echo tag.
<div id="post-<?= $post['id'] ?>"></div>