在包含的文件中使用PHP变量会在包含的文件中计算为NULL

在包含的文件中使用PHP变量会在包含的文件中计算为NULL,php,css,Php,Css,我正在尝试使用PHP处理一些CSS。我想处理的CSS是特定于页面的,因此我想在index.php中设置一个变量,以便在设置变量时只回显CSS index.php <?php $name = 'index'; ?> <?php include 'inc/header.php'; ?> <head> <meta charset="utf-8"> <title></title>

我正在尝试使用PHP处理一些CSS。我想处理的CSS是特定于页面的,因此我想在
index.php
中设置一个变量,以便在设置变量时只回显CSS

index.php

<?php
    $name = 'index'; 
?>
<?php
    include 'inc/header.php';
?>
<head>
      <meta charset="utf-8">
      <title></title>       
<?php include 'import.php'; ?>
</head>
<link rel="stylesheet" href="css/styles.css.php" type="text/css" />
.jumbotron {
    background-image: url(../img/jumbotron.jpg);
    <?php 
        if ($name === "index") { 
            echo("height: 50VH;"); /* Does not echo anything*/
        }
        var_dump($name); // Evaluates to NULL
    ?>
}

header.php

<?php
    $name = 'index'; 
?>
<?php
    include 'inc/header.php';
?>
<head>
      <meta charset="utf-8">
      <title></title>       
<?php include 'import.php'; ?>
</head>
<link rel="stylesheet" href="css/styles.css.php" type="text/css" />
.jumbotron {
    background-image: url(../img/jumbotron.jpg);
    <?php 
        if ($name === "index") { 
            echo("height: 50VH;"); /* Does not echo anything*/
        }
        var_dump($name); // Evaluates to NULL
    ?>
}

import.php

<?php
    $name = 'index'; 
?>
<?php
    include 'inc/header.php';
?>
<head>
      <meta charset="utf-8">
      <title></title>       
<?php include 'import.php'; ?>
</head>
<link rel="stylesheet" href="css/styles.css.php" type="text/css" />
.jumbotron {
    background-image: url(../img/jumbotron.jpg);
    <?php 
        if ($name === "index") { 
            echo("height: 50VH;"); /* Does not echo anything*/
        }
        var_dump($name); // Evaluates to NULL
    ?>
}

已为此文件正确设置标头。然后解释CSS

styles.css.php

<?php
    $name = 'index'; 
?>
<?php
    include 'inc/header.php';
?>
<head>
      <meta charset="utf-8">
      <title></title>       
<?php include 'import.php'; ?>
</head>
<link rel="stylesheet" href="css/styles.css.php" type="text/css" />
.jumbotron {
    background-image: url(../img/jumbotron.jpg);
    <?php 
        if ($name === "index") { 
            echo("height: 50VH;"); /* Does not echo anything*/
        }
        var_dump($name); // Evaluates to NULL
    ?>
}
.jumbotron{
背景图片:url(../img/jumbotron.jpg);
}
如何确保将
$name
设置为它的值,如
index.php
中设置的那样



EDIT:我的最终解决方案是从
stdClass
创建一个对象,并将我需要的变量作为属性添加到该对象中。将GET请求放在指向CSS文件的链接中,CSS文件是对象JSON字符串的Base64。

问题在于,您是从呈现的html内部链接到该style.CSS.php。这意味着页面将在一个单独的请求中获取(当您检查页面时,您可以使用se)。此请求永远不会通过index.php,因此不会设置
$name
变量

我建议您在样式块中的呈现HTML中包含css。将import.php更改为以下内容应该很容易:

<style>
<?php include 'css/styles.css.php' ?>
</style>

问题是您正在从呈现的html内部链接到style.css.php。这意味着页面将在一个单独的请求中获取(当您检查页面时,您可以使用se)。此请求永远不会通过index.php,因此不会设置
$name
变量

我建议您在样式块中的呈现HTML中包含css。将import.php更改为以下内容应该很容易:

<style>
<?php include 'css/styles.css.php' ?>
</style>

文件
styles.css.php
不知道
$name
变量-这就是为什么在执行
var\u dump($name)
时它为空。使用
只需将style.css.php的内容输出为纯html/css。您需要执行
include_once('styles.css.php')
require_once('styles.css.php')
,以便正确计算
$name
变量。

文件
styles.css.php
不知道
$name
变量-这就是为什么在执行
var\u转储($name)
时它是空的。使用
只需将style.css.php的内容输出为纯html/css。您需要执行
include_once('styles.css.php')
require_once('styles.css.php')
,以便正确计算
$name
变量。

标记对一个文件发出HTTP请求,该请求完全独立于包含彼此的php页面,因此,
$name
在styles.css.php中不可用

要这样做,您需要像这样链接样式表,以将
$name
作为get变量传递:

<link rel="stylesheet" href="css/styles.css.php?name=<?php echo $name; ?>" type="text/css" />
标记对文件发出HTTP请求,该请求完全独立于包含彼此的PHP页面,因此
$name
在styles.css.PHP中不可用

要这样做,您需要像这样链接样式表,以将
$name
作为get变量传递:

<link rel="stylesheet" href="css/styles.css.php?name=<?php echo $name; ?>" type="text/css" />

您有一个由
index.php
生成的HTML页面。这会告诉浏览器在
行加载外部样式表。通过浏览器加载外部数据是一个全新的要求。因此,生成样式表的PHP文件不知道任何关于为HTML页面提供服务的脚本中声明的变量的信息

实际上,变量$name是未设置的。由于它是通过引用
var\u dump
作为参数给出的(内置函数声明为通过引用获取参数),因此引用会导致
null

如果您只想在一个PHP脚本中区分不同的样式表,请将GET参数作为查询附加到URL:

<link rel="stylesheet" href="css/styles.css.php?name=<?PHP echo $name;?>" type="text/css" />

您拥有一个由
index.php
生成的HTML页面。这会告诉浏览器在
行加载外部样式表。通过浏览器加载外部数据是一个全新的要求。因此,生成样式表的PHP文件不知道任何关于为HTML页面提供服务的脚本中声明的变量的信息

实际上,变量$name是未设置的。由于它是通过引用
var\u dump
作为参数给出的(内置函数声明为通过引用获取参数),因此引用会导致
null

如果您只想在一个PHP脚本中区分不同的样式表,请将GET参数作为查询附加到URL:

<link rel="stylesheet" href="css/styles.css.php?name=<?PHP echo $name;?>" type="text/css" />

尝试使用全局$name是否将index.php包含到styles.css.php中?这是因为styles.css.php中未定义
$name
。您还应该启用错误报告,因为这应该会产生一个警告。@LorenceHernandez修复了它,但现在我的CSS文件中有一个完整的PHP文件。这不是我真正想要的。尝试使用全局$name是否将index.php包含到styles.css.php中?这是因为styles.css.php中没有定义
$name
。您还应该启用错误报告,因为这应该会产生一个警告。@LorenceHernandez修复了它,但现在我的CSS文件中有一个完整的PHP文件。这不是我真正想要的,修好了!非常感谢:-)修好了!非常感谢:-)