PHP:将函数的输出插入到字符串中

时间:2012-12-30 06:53:05

标签: php function

我正在编写一个自定义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问题。)

2 个答案:

答案 0 :(得分:3)

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

此时ECHO函数将获取该文本并将其输出到文本正文,其中返回将该数据发送回调用它的内容,在您的情况下,将其添加到{{{ 1}}变量。

$css_bgcolor_height_embeded

答案 1 :(得分: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在评估时将其打印出来,