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

PHP:将函数的输出插入字符串,php,function,Php,Function,我正在编写一个自定义PHP函数,以便更容易地生成使用媒体查询的CSS样式 我的函数变得非常重复,所以我创建了第二个函数,我把它放在第一个函数中。第二个函数包含所有重复代码 第二个函数生成一个文本字符串,插入第一个函数的另一个字符串中 但是,我发现最终输出将第二个函数文本字符串放在第一个函数字符串的外部。我怎样才能纠正这个问题 这是我的密码: <?php function css_bgcolor_height_embded($region, $height_value, $mobil

我正在编写一个自定义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');

?>
以下是输出内容:(媒体查询的代码应放在其中)


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

除了在
css\u height\u输出
函数中使用
echo
外,还应该使用return。问题是,
echo
将立即输出该字符串,而不是将其插入您要查找的字符串中

ECHO函数将在此时获取该文本并将其输出到文本体,其中as return将该数据发送回所称的内容,在您的情况下,将其添加到嵌入的
$css\u bgcolor\u height\u
变量的末尾

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;
    } ";

}

除了在
css\u height\u output
函数中使用
echo
外,还应该在其位置使用return。问题是,
echo
将立即输出该字符串,而不是将其插入您要查找的字符串中

ECHO函数将在此时获取该文本并将其输出到文本体,其中as return将该数据发送回所称的内容,在您的情况下,将其添加到嵌入的
$css\u bgcolor\u height\u
变量的末尾

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;
    } ";

}
试试这个

<?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');

?>

注意使用返回而不是回音。Echo会在评估时打印出来,

试试这个

<?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');

?>


注意使用返回而不是回音。Echo在评估时将其打印出来,而不是在函数中调用数据,将其返回到变量,然后在适当的地方回显变量。除非我误解了你的问题^。^@MattClark将此作为回答。
$var.=我的函数(…)
其中
my_函数(..)
返回字符串,而不是
echo
在函数中调用数据,将其返回到变量,然后在适当的位置回显变量。除非我误解了你的问题^。^@MattClark将此作为回答。
$var.=我的函数(…)其中
my_函数(..)
返回字符串谢谢!我真不敢相信一个小小的字就让我如此悲伤!你会惊讶于我有这么多的书信问题。。。xD快乐编码!谢谢我真不敢相信一个小小的字就让我如此悲伤!你会惊讶于我有这么多的书信问题。。。xD快乐编码!谢谢还感谢\n建议使输出更具可读性。谢谢!还感谢\n建议使输出更具可读性。