0

我正在编写一个自定义 PHP 函数,以便更轻松地生成使用媒体查询的 CSS 样式。

我的函数变得非常重复,所以我创建了第二个函数,我将它放在第一个函数中。第二个函数包含所有重复的代码。

第二个函数生成一个文本字符串,该字符串插入到第一个函数的另一个字符串中。

但是,我发现最终输出将第二个函数文本字符串放在第一个函数字符串之外。我该如何纠正?

这是我的代码:

    <?php
function css_bgcolor_height_embded($region, $height_value, $mobile_setting, $mobile_custom_height)
{
    function css_height_output($region, $height_value, $class_prefix, $height_division)
    {
        echo "$class_prefix" . " ." . $region . "-bgcolor-height-source-element {
            height: $height_value" . "px;
            height:" . (($height_value / $height_division) / 10) . "rem;
        }" . "   " . "

        $class_prefix" . " ." . $region . "-bgcolor-height  {
            top:" . "$height_value" . "px;
            top:" . (($height_value / $height_division) / 10) . "rem;
        } ";

    }


    $css_bgcolor_height_embeded .= css_height_output($region, $height_value, '', 1);


    if ($mobile_setting == '50%') {
        $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $height_value, '.submenu-devices', 2) . "}";

        $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $height_value, '.submenu-portables', 2) . "}";

        $css_bgcolor_height_embeded .= css_height_output($region, $height_value, 'submenu-all', 2);


    } elseif ($mobile_setting == 'custom') {
        $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $mobile_custom_height, '.submenu-devices', 2) . "}";

        $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $mobile_custom_height, '.submenu-portables', 2) . "}";

        $css_bgcolor_height_embeded .= css_height_output($region, $mobile_custom_height, '.submenu-all', 2);

    }

    echo $css_bgcolor_height_embeded;
}

echo css_bgcolor_height_embded('header', 10, 'custom', '100');

?>

这是我的输出:(媒体查询的代码被错误地放置在它之外[见结尾])

.header-bgcolor-height-source-element {
    height: 10px;
    height: 1rem;
}

.header-bgcolor-height {
    top: 10px;
    top: 1rem;
}

.submenu-devices .header-bgcolor-height-source-element {
    height: 100px;
    height: 5rem;
}

.submenu-devices .header-bgcolor-height {
    top: 100px;
    top: 5rem;
}

.submenu-portables .header-bgcolor-height-source-element {
    height: 100px;
    height: 5rem;
}

.submenu-portables .header-bgcolor-height {
    top: 100px;
    top: 5rem;
}

.submenu-all .header-bgcolor-height-source-element {
    height: 100px;
    height: 5rem;
}

.submenu-all .header-bgcolor-height {
    top: 100px;
    top: 5rem;
}
@media only screen and (min-width: 0px) and (max-width: 959px) {
}
@media only screen and (min-width: 0px) and (max-width: 767px) {
}

输出应该是这样的:(媒体查询的代码应该放在里面)

.header-bgcolor-height-source-element {
    height: 10px;
    height: 1rem;
}

.header-bgcolor-height {
    top: 10px;
    top: 1rem;
}


.submenu-all .header-bgcolor-height-source-element {
    height: 100px;
    height: 5rem;
}

.submenu-all .header-bgcolor-height {
    top: 100px;
    top: 5rem;
}
@media only screen and (min-width: 0px) and (max-width: 959px) {

.submenu-devices .header-bgcolor-height-source-element {
    height: 100px;
    height: 5rem;
}

.submenu-devices .header-bgcolor-height {
    top: 100px;
    top: 5rem;
}

}
@media only screen and (min-width: 0px) and (max-width: 767px) {
    .submenu-portables .header-bgcolor-height-source-element {
    height: 100px;
    height: 5rem;
}

.submenu-portables .header-bgcolor-height {
    top: 100px;
    top: 5rem;
}
}

(最终代码将被插入到 Drupal 函数中。但是,我忽略了 Drupal 元素,因为我认为这更像是一个通用的 PHP 问题)。

4

2 回答 2

3

echo除了在你的函数中使用css_height_output,你应该使用 return 代替它。问题是echo它将立即输出该字符串,而不是将其插入到您要查找的字符串中。

ECHO 函数将在此时获取该文本并将其输出到文本的主体,其中 return 会将数据发送回调用它的对象,在您的情况下,将其添加到$css_bgcolor_height_embeded变量的末尾。

function css_height_output($region, $height_value, $class_prefix, $height_division)
{
    return "$class_prefix" . " ." . $region . "-bgcolor-height-source-element {
        height: $height_value" . "px;
        height:" . (($height_value / $height_division) / 10) . "rem;
    }" . "   " . "

    $class_prefix" . " ." . $region . "-bgcolor-height  {
        top:" . "$height_value" . "px;
        top:" . (($height_value / $height_division) / 10) . "rem;
    } ";

}
于 2012-12-30T06:57:11.593 回答
2

试试这个...

<?php
function css_bgcolor_height_embded($region, $height_value, $mobile_setting, $mobile_custom_height)
{
    function css_height_output($region, $height_value, $class_prefix, $height_division)
    {
    return "$class_prefix" . " ." . $region . "-bgcolor-height-source-element {
        height: $height_value" . "px;
        height:" . (($height_value / $height_division) / 10) . "rem;
    }" . "   " . "

    $class_prefix" . " ." . $region . "-bgcolor-height  {
        top:" . "$height_value" . "px;
        top:" . (($height_value / $height_division) / 10) . "rem;
    }\n\n";

    }


    $css_bgcolor_height_embeded = css_height_output($region, $height_value, '', 1);


    if ($mobile_setting == '50%') {
    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $height_value, '.submenu-devices', 2) . "}";

    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $height_value, '.submenu-portables', 2) . "}";

    $css_bgcolor_height_embeded .= css_height_output($region, $height_value, 'submenu-all', 2);


    } elseif ($mobile_setting == 'custom') {
    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $mobile_custom_height, '.submenu-devices', 2) . "}";

    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $mobile_custom_height, '.submenu-portables', 2) . "}";

    $css_bgcolor_height_embeded .= css_height_output($region, $mobile_custom_height, '.submenu-all', 2);

    }

    return $css_bgcolor_height_embeded;
}

echo css_bgcolor_height_embded('header', 10, 'custom', '100');

?>

注意使用return而不是echo。Echo 在评估时将其打印出来,

于 2012-12-30T07:05:54.603 回答