简体   繁体   中英

incorprating css and html elements into php

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 :)

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.

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

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 } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM