Php 如何在使用输出缓冲时避免变量冲突

Php 如何在使用输出缓冲时避免变量冲突,php,output-buffering,Php,Output Buffering,作为一个学习练习,我正在编写一个函数,用于组合和缩小css文件和输出生成的css的php文件 在css文件中循环一切正常,但只要它尝试从php文件推送输出缓冲区返回的字符串,数组就会变成int。var_dump()产生以下结果: int(5) 我还尝试连接字符串;它再次正常工作,直到进入php文件,然后字符串中前面的所有内容都变为4。像这样: 4/* * Home: Appointment Grid */ .difAppointmentGrid { margin: 2rem 0;

作为一个学习练习,我正在编写一个函数,用于组合和缩小css文件和输出生成的css的php文件

在css文件中循环一切正常,但只要它尝试从php文件推送输出缓冲区返回的字符串,数组就会变成int。var_dump()产生以下结果:

int(5)
我还尝试连接字符串;它再次正常工作,直到进入php文件,然后字符串中前面的所有内容都变为4。像这样:

4/*
* Home: Appointment Grid
*/
.difAppointmentGrid {
    margin: 2rem 0;
    font-family: "Lato" !important;
    box-shadow: -4px 4px 16px 4px hsla( 240, 0%, 0%, 0.1 );
}
. . . 
这是我在styles.php文件中所做工作的一个示例:

. . . 
.difAppointmentGrid header div h3 {
    margin: 0;
    padding: 1.5rem 0;
    font-size: 2rem;
    text-align: center;
    color: white;
}
<?php
for ( $h3 = 1, $o = 0.40; $h3 <= 4; ++$h3, $o += 0.20 )
{
    $rule = '.difAppointmentGrid header div:nth-child('.$h3.') h3 {'."\n\t".
            'background-color: hsla( 223, 63%, 22%, '.$o.' );'."\n".
            '}'."\n";
    echo $rule;
}
?>
.dif_grid {
    display: flex;
}
. . . 
最奇怪的是,当数组推送/连接时,实际上没有抛出错误。我甚至不知道该问什么问题,因为我无法真正找出问题所在。我还搞乱了标题、字符编码、不同的ob函数,以及在绝望中抛出ob_get_flush来字符串

解决方案:

function get_include_output($file)
{
    ob_start();
    include( $file );
    return ob_get_flush();
}
function styles_init()
{
    $path = __DIR__ . '/aggregate.min.css';
    if ( is_writable( $path ) )
    {
        $r = array();
        foreach( array_filter( glob( __DIR__ . '/modules/*.*' ), 'is_file' ) as $file )
        {
            $fn = pathinfo( $file );
            if ( $fn['extension'] == 'php' || $fn['extension'] == 'css'  )
            {
                $r[] = get_include_contents( $file );
            }
        }
        $c = implode( "\n\n", $r );
        //$c = str_replace( array( ' {', ': ', ', ' ), array( '{', ':', ',' ) , str_replace( array( "\r\n", "\r", "\n", "\t", '  ', '    ', '    ' ), '', preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $c ) ) );

        $f = fopen( $path, 'wb' );
        fwrite( $f, $c );
        fclose( $f );
    }
}

我怀疑您包含的PHP文件使用了变量
$r
,因此它会用一个数字覆盖您的变量。您可以通过包装代码来避免变量冲突,该代码获取将文件作为字符串包含在函数中的结果,因为这将有自己的变量范围

function get_include_output($file) {
    ob_start();
    include($file);
    return ob_get_flush();
}
然后将代码更改为:

       if ( $fn['extension'] == 'php' || $fn['extension'] == 'css'  )
        {
            $x = get_include_contents($file);
            array_push( $r, $x );
        }

你在哪里倾倒?另外,
$r[]=$x
比在这里使用
array\u push()
更好。@GentlemanMax我刚刚用$r[count($r)]=$x尝试过;这导致了同样的行为。我刚刚使用var_dump()和error_log()在循环时获取值。我怀疑这是由于我不知道的输出缓冲的某些方面造成的。您不需要
count($r)
位,不管怎样,您正在
var\u dump($r)
之后执行
array\u push()
?您是否尝试过使用
ob\u get\u clean()
?实际上应该返回array@GentlemanMax是的,关于这两个。实际上,我是用ob_get_clean()编写函数的,但是我将其切换为ob_get_flush(),因为后者工作更可靠,即ob_get_clean()只返回style.php的内容,而忽略任何*.css文件;而ob_get_flush()返回两种文件类型的内容;我没有意识到php没有块级别的作用域。我星期一回到办公室时必须核实一下。如果这是原因,那么这意味着输出缓冲不提供范围封装。正确吗?记住,include文件的一个常见用途是设置变量。如果include文件在它自己的范围内,它就不能这样做。我刚刚开始学习PHP,所以这些都是非常有用的信息。谢谢
       if ( $fn['extension'] == 'php' || $fn['extension'] == 'css'  )
        {
            $x = get_include_contents($file);
            array_push( $r, $x );
        }