Paulo Bueno
Paulo Bueno

Reputation: 2569

PHP HTML output

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 ...

Upvotes: 0

Views: 1360

Answers (3)

Andy Baird
Andy Baird

Reputation: 6208

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>";

Upvotes: 1

Gazler
Gazler

Reputation: 84150

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

Upvotes: 2

philfreo
philfreo

Reputation: 43804

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

Upvotes: 0

Related Questions