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