如何在 PHP 中声明一个可以跨模板使用的全局变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12638734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I declare a global variable in PHP I can use across templates?
提问by Claire
In wordpress, it calls header.php then whatever template name or the page requested. How can I declare a variable in my php header which I can then refer to in my other templates?
在 wordpress 中,它调用 header.php 然后调用任何模板名称或请求的页面。如何在我的 php 标头中声明一个变量,然后我可以在其他模板中引用它?
回答by Gerald Schneider
The answer pretty much depends what you need it for. If you are developing a theme and want to keep values constant through all files you can place them in functions.phpin the theme directory, which is always loaded. Variables defined there should be available everywhere in the theme. This works if you distribute the theme.
答案在很大程度上取决于您需要它做什么。如果您正在开发一个主题并希望通过所有文件保持值不变,您可以将它们放在functions.php主题目录中,该目录始终加载。在那里定义的变量应该在主题的任何地方都可用。如果您分发主题,这将起作用。
If you want to modify an existing theme for your needs on your own installation, you can either put them in wp-config.php, as suggested, or (a cleaner method) you can create a child themeof the theme you want to change. This will keep it separate from the wordpress core and will prevent theme updates from overwriting your changes.
如果您想根据自己的安装需要修改现有主题,您可以wp-config.php按照建议将它们放入,或者(更简洁的方法)您可以创建要更改的主题的子主题。这将使其与 wordpress 核心分开,并防止主题更新覆盖您的更改。
I just tried it using functions.php:
我只是尝试使用functions.php:
functions.php:
函数.php:
$variable = "value";
header.php:
头文件.php:
global $variable;
echo $variable;
works for me.
为我工作。
回答by SMacFadyen
You can declare it like so (In functions.php):
你可以这样声明(在functions.php中):
global $domain;
$domain = 'http://www.mydomain.com/';
You can echo the domain of the WordPress site like this, though..
不过,您可以像这样回显 WordPress 站点的域。
<?php bloginfo('site_url');?>
Or
或者
$url = site_url();
echo $url;
Or as a function, in functions.php
或者作为函数,在functions.php中
function displayMyDomain() {
global $domain; // you probably don't actually need to set it global as it is a function
$domain = 'http://www.domain.com/';
echo $domain;
}
Then anywhere, use <?php displayMyDomain(); ?>
然后在任何地方,使用 <?php displayMyDomain(); ?>
回答by Mihai Iorga
You can define them in wp-config.phpfile.
您可以在wp-config.php文件中定义它们。
The config file is never updated so your Wordpress installation will have those variables over time even if you update it.
配置文件永远不会更新,因此即使您更新它,您的 Wordpress 安装也会随着时间的推移包含这些变量。
<?php
require_once('wp-config.php');
echo $domain;
?>
回答by Mohamed Salem Lamiri
Another way to define global consts in Wordpress, if for best practice purpose you don't want to add a variable to your WP-config (to keep it clean ?). And for some reasons your variable scope is not getting the global scope everywhere in your website.
在 Wordpress 中定义全局常量的另一种方法,如果出于最佳实践的目的,您不想向 WP-config 添加变量(以保持其干净?)。并且由于某些原因,您的变量范围没有在您网站的任何地方获得全局范围。
You can simply use :
您可以简单地使用:
define( 'MY_VARIABLE', 'My Value' );
For more information about consts scope : http://php.net/manual/en/language.constants.php
有关常量范围的更多信息:http: //php.net/manual/en/language.constants.php
回答by Dmitry
I think php sessionis one of the way to use as a global variable on Wordpress.
我认为php session是在Wordpress上用作全局变量的方法之一。
1. Functions.php
1. 函数.php
if(!session_id()) {
session_start();
}
2. Any .phpfile where you want set or get global variables
2. 任何.php你想设置或获取全局变量的文件
get variable
if(isset($_SESSION['global_var'])) { $local_var = $_SESSION['global_var']; }set variable
$_SESSION['global_var'] = "This is global var";
获取变量
if(isset($_SESSION['global_var'])) { $local_var = $_SESSION['global_var']; }设置变量
$_SESSION['global_var'] = "This is global var";

