在 header.php 中设置变量但在 footer.php 中看不到 [英] setting variable in header.php but not seen in footer.php

查看:26
本文介绍了在 header.php 中设置变量但在 footer.php 中看不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 wordpress 中,我在 header.php

in wordpress , i set a variable in header.php

<?php
$var= 'anything'
?>

但是在 footer.php 当我回声时

<?php
echo $var;
?>

我没有打印任何东西......为什么!>

I got no thing printed ... why !>

推荐答案

您不在同一范围内,因为页眉和页脚文件包含在函数主体中.所以你声明了一个局部变量,并引用了另一个局部变量(来自另一个函数).

You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).

所以只需将您的变量声明为全局变量:

So just declare your variable as global:

$GLOBALS[ 'var' ] = '...';

那么:

echo $GLOBALS[ 'var' ];

这篇关于在 header.php 中设置变量但在 footer.php 中看不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆