I make the internet beautiful. Front End Developer with a crush on the back end. Hubspot COS Penguin. Favorite color is #bada55 - I think I may have just told a lie! This purple color is more like my favorite color, I just wanted to write badass

logo

How do I include a file and return it using a function using PHP?

downloadThis is what I searched for all day. I hadn’t ever tried to do it before and here I am trying to make a website and I’m not able to return this to my page properly. I have my pages set up like this:

<html>
    <head>
        <meta charset="UTF-8">
        <meta description="Cooking Classes">
        <title>Chef Website</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <link href="http://fonts.googleapis.com/css?family=Josefin+Slab:400,600,700" rel="stylesheet" type="text/css">
    </head>
    <body>
    <?php 
        require_once 'includes.php';   
        echo Chef::getHeader();
        echo Chef::getSlider('Home');
        ?>
<!--That is all we really need to see for the example
That is the basic breakdown of what I am doing. I’m requiring my includes file, then I’m running functions inside of it and returning html to the website. This is what the includes.php files looks like:

<?php
class Chef {
     public static function getContent($headerTitle, $pageName) {
        $title = '<div id="contentTitle"><h2>'.$headerTitle.'</h2></div>';
        $contentTop = '<div id="contentText"><p>';
        $contentBottom = '</p></div>';
        ob_start();
            include 'content/'.$pageName.'.txt';
        return $title.$contentTop.ob_get_clean().$contentBottom;
        
    }
}
What happens here is the text file I include is put into the output buffer [ob_start()]. AHHH. That was the moment. I had to put it in the output buffer to keep it from displaying directly on the screen. I can now access the content by using the function ob_get_clean(). Is there another way to do this? What works best for you? And always, if this code is crap, please be constructive — I don’t know everything that you know! Thanks and hope this helps you!

Trackback from your site.

Leave a comment

What about a meepface?

Somebody’s Instagram Feed