0

I've done a project that now needs to be changed in order to display one div if a variable is in an array and a different div if it isn't in the array.

Normally I'd just do

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

and be on my merry way, however the code I've got already contains a load of html and php code already, which doesn't like encapsulated within another PHP block (as far as I'm aware?) e.g.

<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "Quarter HTML CODE
<?php quarter phpcode ?>";}
else
{echo "Non-Quarter HTML CODE
<?php non-quarter phpcode ?>";}
?>  

So my question is, what is the best way to tackle this? Is it simply to do a javascript hide div A when the variable is met and hide divB when the variable isn't met, or is there a better solution?

Thanks

1
  • You can also have a string variable holding only these divs, like "output part 1".$div_variable."output part 2" Commented Apr 16, 2013 at 11:23

2 Answers 2

3
<?php
if (in_array($month,$quartermonths))
{ ?>

Quarter HTML CODE
<?php quarter phpcode ?>

<?php } ?>

split your html code from php code like this.

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

1 Comment

Such an obviously simple answer and yet exactly what I'm looking for! Trying to keep tabs on the start and ends of blocks of code was never my strong point :D
0

It sounds like you just want to concatenate the value of quarter phpcode. Let's say it's a single function, quarter_phpcode(). You can just do this:

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

2 Comments

So create the PHP blocks as functions and then call them within the if else?
@sam.clements I'm not saying that, I'm just saying that the result of whatever php code you have can be concatenated to the string. You could also break out of php like the other answer suggests, but using echo will work just fine too

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.