0

I would like to write a function to return a string with html code to customize head title, description and keywords for multiple pages. I started with my index.php file and two auxiliary, _head.php and _functions.php. What do I have to do to implement this function?

index.php:

<?php include "_functions.php; ?>
<html>
<?php echo make_head("My title", "My description", "My keywords); ?>
<body>
...
</body>
</html>

_functions.php:

function make_head(title, description, keywords) {
    return file_get_contents("_head.php");
}

_head.php

<head>
...
<meta name="description" content="$description">
<meta name="keywords" content="$keywords">
<title>$title</title>
...
</head>
1
  • 1
    If you have lots of scenarios like this and if you are okay to use some 3rd part libraries, you can use some templating engines like mustache, smarty, etc... If it is for only one time need, you can go with answers provided by Saurabh and Vasil Shaddix. Commented Aug 11, 2016 at 10:35

3 Answers 3

2

_functions.php:

function make_head($title, $description, $keywords) {
    $head = include "_head.php";
    return $head
}

_head.php

<head>
...
<meta name="description" content="<?php echo $description; ?>" >
<meta name="keywords" content="<?php echo $keywords; ?>" >
<title><?php echo $title; ?></title>
...
</head>
Sign up to request clarification or add additional context in comments.

1 Comment

This is somewhat misleading as the OP's make_head function return a string. And this one appears to do likewise, however it returns the return of the include statement.
1

You can use include.

In your function make_head, you can do something like this:

function make_head(title, description, keywords) {
    $html = include "_head.php";
    return $html;
}

When you include something, it loads it to your current state. So if you use $title or $description or $keywords in your _head.php file, they will be in the same scope and they can be used.

3 Comments

This is somewhat misleading as the OP's make_head function return a string. And this one appears to do likewise, however it returns the return of the include statement.
What ? I don't get what you are saying.
From the manual: Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1. In other words your make_head function doesn't return any html, more likely 1.
-1

This code has a number of vulnerabilities and including files like this is never a good idea. I can see this code is only entry level though and probably just for practice so to get this working you need to do something like the following:

<?php include "_functions.php; ?>
<html>
<?php echo make_head("My title", "My description", "My keywords); ?>
<body>

Then in your make_head function, return the HTML code

function make_head(title, description, keywords) {
    return "<head>
             ...
            <meta name='description' content=' . "$description" .'>
            <meta name='keywords' content=' . "$keywords" . '>
            <title>$title</title>
            ...
            </head>";
}

This approach leave you open and I certainly wouldn't use it in production.

1 Comment

I figure, it's in part the copied syntax error. The statement 'including files like this is never a good idea', a potential insult that the code is entry level. Not pointing out what the vulnerabilities could potentially be, and producing a function that has syntax errors and is probably harder to edit for a front end developer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.