What i'm i doing wrong? here is my code

FUNCTIONS.PHP

<?php
function get_header(){
    include 'header.php';
}
function get_footer(){
    include 'footer.php';
}

HEADER.PHP

<?php function set_title($title){
    echo $title;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php set_title();?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

HOMEPAGE.PHP

<?php 
require 'functions.php';
$title = ("homepage");
get_header(); 

php get_footer(); ?>
Member Avatar for diafol

SOrry rhod. But I fail to see the point of this.

Perhaps you'd be better off building a pagemanager class. From your code you look to be supplying the data from the page itself, which makes me think that you may as well be providing static markup.

I don't see the need for functions.php here.

I'd do homepage.php like this:

<?php 
$title = "homepage";
include 'header.php';

echo "<div>page body here</div>";

include 'footer.php';
?>
Member Avatar for diafol

Yes, thaat's the point. Unless you're running everything through a front "index.php" page or a page builder, or a template engine, there's little for php to actually do here.

If I am guessing correctly, you are just trying to get some basics to work before you implement the actual functionality. There's nothing wrong with that!

The problem basically is just that you need to declare the $title variable as global in each of the functions that references it. I would do away with the set_title function completely and change get_header:

<?php
function get_header(){
    global $title;
    include 'header.php';
}
function get_footer(){
    include 'footer.php';
}

Then in header.php:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title; ?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

The key thing to remember is that when you include() or require() a file, the code in it is processed exactly as if you had pasted in that code in the same position as the include() call in the parent file. In this case, that means that in order to access the $title variable you either have to use a global or pass it in as an argument.

A better approach
You may want to consider not relying on globals at all, however, in which case you would want to pass the page data to the get_header() and get_footer() functions like this:

<?php
/**
 * Render the header markup for a basic page.
 * @param $page array of data for the page.
 * @return nothing
 */
function get_header($page) {
    include 'header.php';
}

/**
 * Render the footer markup for a basic page.
 * @param $page array of data for the page.
 * @return nothing
 */
function get_footer($page) {
    include 'footer.php';
}

I added some phpdoc comments, always a good idea with any function (but never required for the code to work, it's more for you later to remember how it works).

Some might argue that you don't need the get_header() and get_footer() functions, and in fact that's correct, but I like making things flexible. If in the future you want to modify how this works (for instance, maybe you want to store the header in mecached to save on database calls or something), you can do so more easily by putting it in a function and there is no harm in doing that.

Anyway, with the above changes header.php would then use an array index into the $page array:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $page['title']; ?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

Then in homepage.php you would do something like this to call the new functions:

<?php 
require_once "functions.php";

// Setup page data
$title = array(
    "title" => "homepage",
    "body" => "Body text",
);

// Render the page header
get_header($page); 

// Other render calls as needed for the particular page

// Render the page footer
get_footer($page);
Member Avatar for diafol

That's a lot of work ^^ for an assumption, heh heh. Perhaps we should wait for the OP to reply.

Actually, the problem exist in the coding itself. As you see

<?php function set_title($title){
    echo $title;
}
?>

The function expect a parameter and the code calling it is <title><?php set_title();?></title> did not parse in the parameter to the function for it to echo.

Member Avatar for diafol

I didn't go that far. It was a mess.

I appreciate both Isaac 4 and ips for understanding this Simple code and its use. Isaac 4 thank you for all the time you spent on it and it worked flawlessly, i didn't think of creating a global variable, I kept tying to pass it in the get header function function get_header($title){ include "header.php";}. THANKS!!

However diafol was right about a pagemanager class, still not good with PHP OOP but I'll dive into it , see what i can come up with and come back here if I get stuck.

includer.php

<?php
Class Includer {


function get_header($title){

    include 'header.php';
}
function get_footer(){
    include 'footer.php';
}
}

Header.php

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title; ?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

Homepage.php

<?php 
require_once "includer.php";
$includer = new Includer;

$includer->get_header("homepage"); 

$includer->get_footer(); ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.