0

What is the correct way to output html inside a switch structure?

I've tried both below, none work:

<?php
switch($x){

case "a":
$var = <<< EOM;
...the html...
EOM;
break;

case "b":
...some code...
break;

}
?>

OR <?php switch($x){

case "a":
?>
...the html...
<?php
break;

case "b":
...some code...
break;

}
?>

More info:

switch ($_REQUEST['act']){

    case 'editdiretorio':
    
    $sql="select * from diretorio where did=$_GET[edit]";
    $row=mysql_fetch_assoc(mysql_query($sql));
    
    ?>
    <h1>Cadastro de Serviço</h1>
    <form id="fdiretorio" method="post" action="diretorioconfirm.php">
    <?php if($edit){echo "<input name='act' type='hidden' value='update'>";?>
           Nome completo de quem preenche o questionário:<br />
      <input type="text" name="dnome" class="form-default" style="width:200px;" value="<?php echo "$row[dnome]";?>"/>
      <br />

... more 400 lines of code ...

<?php
break;

case "b":
... other code ...
2
  • what do you mean by "don't work"? By the looks of it the second example should work. The first one should also work, you'r just not echoing the HTML ... I think you'll have to give us more information ... Commented Dec 9, 2009 at 22:15
  • Ok there was an sintax error. I'm truly sorry. Commented Dec 9, 2009 at 22:25

3 Answers 3

2

There is no correct way. Outputting HTML within a switch is the same as outputting it anywhere else.

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

Comments

1

There's no correct way perse, both should work.

In your first example you need to make sure there is no space between <<< and EOM and no semicolon afterwards.

Example:

$myvar = <<<END
<div>My html here</div>
END;
echo $myvar;

another option would be to use:

echo "<div>My html here</div>";

1 Comment

I would definitely vote for the first option. It tends to be a lot less messy when outputting a lot of HTML. Of course, if you are going to be working with large amounts of HTML, a template engine like Smarty makes things a LOT cleaner.
0

The second example should work, except you said <php instead of <?php

2 Comments

Yup, and the first one doesn't generate an output... it's just storing the value in a database
Also, in the second case, he forgot <?php as well

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.