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).
echoing your data in the functions, return it to a variable, and then echo the variable where appropriate. Unless I am misunderstanding your problem ^.^$var .= my_function(..);in whichmy_function(..)returns a string