Ok, so far this is getting quite fustrating, I am trying to make a very very basic template system to take out some of the repetitive coding for my projects but this code is not working at all. Global.php <?php $header = include('templates/header.php'); ?> PHP: index.php <?php $header ?> PHP: header.php This is the header. PHP: What is output This is the header. 1 Where is that one coming from, I have several other templates and every time I add them with the same procedure it will display the code in the wrong display order and always add an extra 1 at the end of the page.
maybe <?php $header = file_get_contents('templates/header.php'); ?> PHP: Or AFAIK include can return values but you need to use return keyword inside of included files.
Just put <?php include('templates/header.php'); ?> Code (markup): in index.php. You may need to adjust the path for "templates/header.php" though. In your case you can even just put <?php include('templates/header.php'); ?> Code (markup): in Global.php. Although that is not recommended though.
yes but I am working with a group of people who no nothing of web design but I am supposed to work with them for a school project. Instead of having them insert that php statement I would want to just make it so all they had to do was insert the $header instead of the $header = includes('templates/header.php');
if that's the case then you can use Ashine's code in Global.php <?php $header = file_get_contents('templates/header.php'); ?> Code (markup): Then use this in index.php <?php echo $header; ?> Code (markup): This works if header.php only contains text and without any PHP code. If you have php code inside header.php, then you can do something like this in Global.php <?php ob_start(); include('templates/header.php'); $header = ob_get_contents(); ob_end_clean(); ?> Code (markup): Hope that helps
Thank you, your 2nd solution was the exact thing i needed. Now just gotta figure out a more effective method to do this for about 50 different includes, or do I have to do this for each one? any way I can make this automatically do this for all files in a directory? <?php ob_start(); include('templates/header.php'); $header = ob_get_contents(); ob_end_clean(); ?> PHP:
Yes you can. You can even do it if its in different directory. Just need to add a static path or a relative path to it. If all your files already included Global.php, then you only need to put <?php echo $header; ?> on each file. e.g. index.php <?php include('Global.php'); echo $header; ?> Code (markup):