0

I'm writing a custom PHP function to make it easier to generate CSS styles that use media queries.

My function was getting quite repetitive, so I created a second function, that I placed inside the first function. The second function contained all the repetitive code.

The second function produces a string of text, which is inserted inside another string from the first function.

However, I find the final output places the second function text string outside of the first function string. How can I correct this?

Here is my code:

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

?>

Here is my output: (The code for the media query is wrongly placed outside of it [See end])

.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) {
}

Here is what the output should be: (The code for the media query should be placed inside of it)

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

(The final code will then be inserted into a Drupal function. However, I have left out the Drupal elements as I think this is more of a generic PHP problem).

3
  • 1
    Instead of echoing your data in the functions, return it to a variable, and then echo the variable where appropriate. Unless I am misunderstanding your problem ^.^ Commented Dec 30, 2012 at 6:55
  • @MattClark Post that as an answer. Commented Dec 30, 2012 at 6:56
  • $var .= my_function(..); in which my_function(..) returns a string Commented Dec 30, 2012 at 6:56

2 Answers 2

3

Insead of using echo in your css_height_output function, you should use return in its place. The problem is that echo will immediate output that string, instead of inserting it into the string where you are looking for it to be.

The ECHO function will at that point, take that text and output it to the body of the text, where as return will send that data back to what called it, in your case, adding it to the end of your $css_bgcolor_height_embeded variable.

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I can't believe that one little word caused me so much grief!
You would be surprised at how many letter case proBlems I have had... xD Happy coding!
2

Try this instead...

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

?>

Pay attention to use return instead of echo. Echo prints it out at time of evaluation,

1 Comment

Thanks! Thanks also for the \n suggestion to make the output more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.