Include problem

Hi there,

I am trying to include a file in this line, but it doesn’t seem to be working.

Any ideas what I have wrong?


$content .='<div class="jobs-right">';
$content .= include("sidebar.php");
$content .='</div>';

Thanks

Are you using a templating engine?
Or only pure php as a template?

If pure php only as a template.
Then you codes I guess will never work, because php tags <?php ?> is missing?
And I guess because you are storing it as a string?

Hi,

It is pure PHP and also has opening and closing <?php tags. Is there another way I can write it?

Thanks

Try this which should either produce errors or show the value of $content:



error_reporting(-1);
ini_set('display_errors', true);

$content  = '';
$content .='<div class="jobs-right">'; 
$content .= require("sidebar.php"); 
$content .='</div>';  

echo $content;

die;


Please note that include was changed to require which will produce an error message if the file cannot be loaded,

What does sidebar.php contain? For your current setup to work it should contain a return that returns the contents you want included. If it does not return anything, the string “1” will be added to your string (since include returns “1” by default if you don’t return anything yourself).

So, you code should look something like


$content  = '<div class="sidebar">';
$content .= '<ul>';
$content .= '<li><a href="http://www.google.com/">Google</a></li>';
$content .= '<li><a href="http://www.amazon.com/">Amazon</a></li>';
$content .= '</ul>';
$content .= '</div>';
return $content; // <<== This is important!