在 PHP 文件中存储我自己的超级全局变量?

发布于 2024-11-24 13:57:04 字数 308 浏览 1 评论 0原文

我正在建立一个有数百页的网站。每个页面都有一个标题,例如:

世界上最好的网页 - Stackoverflow

如上所述,每个页面的页面标题后面都会有站点名称。

我想将站点名称存储在某种全局变量中。

这样我就不需要手动更改数百个页面标题,以防站点名称发生更改。

现在我可以将名称存储在文件中并使用 require_one 来包含它。

但我希望有一个更优雅的解决方案。这允许我在 php 文件中存储标题,并以与调用 PHP GET 等相同的方式调用它。

I am building a website that will have hundreds of pages. Each of these pages will have a title, like:

The Best Webpage in the world - Stackoverflow

As above every page will have the sites name following the page title.

I want store the sites name in some sort of global variable.

This is so I will not need to manually change hundred's of page titles in-case the sites name changes.

Now I could just store the name in a file and use require_one to include it.

But I was hoping for a more elegant solution. That allows me to store title within php files, and call it in the same way you call a PHP GET etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2024-12-01 13:57:04

您可以做的最简单的事情(也是我推荐的)是这样的:

// This is included in every page (maybe as part of including config.php or equivalent)
define('SITE_NAME', 'Stackoverflow');

function get_title($title) {
    return sprintf('%s - %s', $title, SITE_NAME);
}

// This is in one of your pages
echo get_title('The best webpage in the world');

查看实际操作.

The simplest you can do (and what I would recommend) is something like this:

// This is included in every page (maybe as part of including config.php or equivalent)
define('SITE_NAME', 'Stackoverflow');

function get_title($title) {
    return sprintf('%s - %s', $title, SITE_NAME);
}

// This is in one of your pages
echo get_title('The best webpage in the world');

See it in action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文