tags:

views:

105

answers:

7

I went through this before posting:

http://stackoverflow.com/questions/1100354/easiest-way-to-echo-html-in-php

And still could make sense of it.

I´m trying to echo this:

<div>
 <h3><a href="#">First</a></h3>
 <div>Lorem ipsum dolor sit amet.</div>
</div>
<div>

And I can´t seem to find a workaround to those "" and ''.

Thanks in advance!!

Trufa

+5  A: 
<?php

echo '<div>
 <h3><a href="#">First</a></h3>
 <div>Lorem ipsum dolor sit amet.</div>
</div>
<div>';

?>

Just put it in single quotes.

William Macdonald
Just like that?? wil thy it out!
Trufa
OMG so simple!! Thank you very much, Worked like a charm!
Trufa
just remember to escape any ' you might have in your html with backslash like this \'
Kamil Szot
+3  A: 

Using the first mechanism given there will do it.

<?php
  ...
?>
<div>
 <h3><a href="#">First</a></h3>
 <div>Lorem ipsum dolor sit amet.</div>
</div>
<div>
<?php
  ...
?>
Ignacio Vazquez-Abrams
+4  A: 

Did you try the heredoc based solution:

echo <<<HTML
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>
HTML;
codaddict
+1  A: 

You need to escape the " so that PHP doesn't recognise them as part of your PHP code. You do this by using the \ escape character.

So, your code would look like this:

echo
    "<div>
        <h3><a href=\"#\">First</a></h3>
        <div>Lorem ipsum dolor sit amet.</div>
    </div>
    <div>"
Saladin Akara
A: 

You have a variety of options. One would be to use PHP as the template engine it is:

<?php 
  // Draw the page
?>
<div>
  <h3><a href="#">First</a></h3>
  <div>Lorem ipsum dolor sit amet.</div>
</div>
<?php
  // Done drawing.
?>

Another would be to use single quotes, which let you leave double quotes unquoted and also support newlines in literals:

<?php
  echo '<div>
  <h3><a href="#">First</a></h3>
  <div>Lorem ipsum dolor sit amet.</div>
</div>';
?>

Another would be to use a HEREDOC, which leaves double quotes untouched, supports newlines, and also expands any variables inside:

<?php
  echo <<<EOS
<div>
  <h3><a href="#">First</a></h3>
  <div>Lorem ipsum dolor sit amet.</div>
</div>
EOS;
?>
Victor Nicollet
+1  A: 

Hello,

If you want to output large quantities of HTML you should consider using heredoc or nowdoc syntax. This will allow you to write your strings without the need for escaping.

echo <<<EOD
You can put "s and 's here if you like.
EOD;

Also note that because PHP is an embedded language you can add it between you HTML content and you don't need to echo any tags.

<div>
    <p>No PHP here!</p>
    <?php
    $name = "Marcel";
    echo "<p>Hello $name!</p>";
    ?>
</div>

Also if you just want to output a variable you should use the short-hand output tags <?=$var?>. This is equivalent to <?php echo $var; ?>.

Alin Purcaru
+1  A: 

Seperating HTML an Php is the best method less confusing and easy to bug

<?php
  while($var)
  {
?>

     <div>
         <h3><a href="User<?php echo $i;?>"><?php echo $i;?></a></h3>
         <div>Lorem ipsum dolor sit amet.</div>
     </div>

<?php
  $i++;
  }
?>
Wazzy