0

I have a bunch of php files that I want to incorporate html (divs) and css (external stylesheets) elements into.

If any one has any tips / ideas on how to go about doing this I'd love to hear them :)

1
  • define your understanding of the word "incorporate" Commented Apr 4, 2011 at 10:44

3 Answers 3

5

You could either :

  • Integrate HTML code in your PHP file,
  • Or have your PHP script echo the HTML content.


With the first solution, your PHP file could look like this :

<?php 
    $var = 10;
    // some PHP code
?>
<div>
    this is some HTML
</div>
<?php
    // and some PHP code again
?>

For more informations about this, see the following section of the manual : Escaping from HTML.


While, with the second solution, your PHP file could look like this :

<?php
    $var = 10;
    // some PHP code

    echo '<div>this is some HTML</div>';

    // and some PHP code again

?>


Basically, you are free to mix HTML and PHP code in the same PHP script : outside of <?php ... ?> tags, things will not get interpreted as PHP code.

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

1 Comment

Additionally, chunks of static HTML, or whole pages, can be included with the include statement. CSS and Javascript could also be incorporated in this way, but as HTML already defines methods for including these assets cacheably, it is not recommended. Finally, a useful syntactic alternative to the second method Pascal mentioned is the heredoc string delimiter.
1

All you need to do is exit php and write html as normal:

<?php
// php code
?>

<div>
  <p>Some html</p>
</div>

<?php
// more php code
?>

Comments

0

Take a look at the PHP alternative syntax for control structures. It is often times more comprehensive when you start mixing with plain HTML:

<?php if(...): ?>

<p>paragraph</p>

<?php endif; ?>

Instead of:

<?php if(...) { ?>

<p>paragraph</p>

<?php } ?>

Comments

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.