Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
堆叠PHP代码?_Php_Javascript_Html - Fatal编程技术网

堆叠PHP代码?

堆叠PHP代码?,php,javascript,html,Php,Javascript,Html,我已经完成了一个项目,现在需要更改它,以便在变量位于数组中时显示一个div,在变量不位于数组中时显示另一个div 通常我会这么做 <?php $quartermonths = array("February","May","August","November"); if (in_array($month,$quartermonths)) {echo "quarter code in here";} else {echo "nonquarter code in here";} ?>

我已经完成了一个项目,现在需要更改它,以便在变量位于数组中时显示一个div,在变量不位于数组中时显示另一个div

通常我会这么做

<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "quarter code in here";}
else
{echo "nonquarter code in here";}
?>

尽管如此,我得到的代码已经包含了大量的html和php代码,这不喜欢封装在另一个php块中(据我所知?) e、 g


所以我的问题是,解决这个问题的最好方法是什么?只是在满足变量时执行javascript隐藏div a,在不满足变量时执行隐藏div B,还是有更好的解决方案


谢谢

听起来您只是想将
quarterphpcode的值串联起来
。假设它是一个函数,
quarter\u phpcode()
。您可以这样做:

{ echo "Quarter HTML CODE" . quarter_phpcode(); }

四分之一HTML代码

像这样将html代码与php代码分开。

您也可以有一个字符串变量,只包含这些
div
s,比如
“输出部分1”。$div\u变量。“输出部分2”
,所以创建php块作为函数,然后在if else?@sam.clements中调用它们,我不是说,我只是说,无论您有什么php代码,其结果都可以连接到字符串。你也可以像另一个答案所建议的那样突破php,但是使用
echo
将很好地解决这个显然很简单的问题,但这正是我想要的!试图记录代码块的开始和结束从来都不是我的强项:D
{ echo "Quarter HTML CODE" . quarter_phpcode(); }
<?php
if (in_array($month,$quartermonths))
{ ?>

Quarter HTML CODE
<?php quarter phpcode ?>

<?php } ?>