user2227690
user2227690

Reputation: 11

Why not use included variables?

This is a rather basic question from a total php newbie so please be patient... Why do most of the tutorials on php site maintenance tell you to use php includes but do not mention using variables? Is there for example something wrong with this one:

<title><?php include 'includes/meta.php';
echo "$title_index";?></title>
<meta name="description" content=<?php include 'includes/meta.php';
echo "$desc_index";?>>

... if compared to this one:

<title><?php include 'includes/sitename.php';?></title>
<meta name="description" content=<?php include 'includes/description.php';>

?

In this particular case wouldn't it be easier to have all the SEO-relevant meta tag content in one file instead of spreading them or parts of them in separate files?

So back to the main point: is there some reason to avoid using a "master file" and thus spreading the included content into multiple files that are included in their totality here and there? Or have I just read the wrong articles?

Upvotes: 1

Views: 87

Answers (2)

000
000

Reputation: 27247

It all comes down to preference. For example Wordpress defined methods in separate files, so it is closer to your first example, like

<?php include 'functions.php' ?>
<title><?= get_title() ?></title>
<body><?= get_body() ?></body>
etc.

In the closed-source projects I work in, we define all the variables like

<?php
$title = 'What';
$body = 'foo';

Then we include the template file from there.

include 'template.php';

Where template looks like

<title><?= $title ?></title>
<body><?= $body ?></body>
etc.

There are many ways to shoot yourself in the foot here. Choose your own adventure!

Upvotes: 1

justderb
justderb

Reputation: 2854

I believe they do it this way so your are dealing with variables instead of straight HTML. This way you have more control over how to display the information.

Upvotes: 0

Related Questions