Search code examples
phphtmlfopenfwritefclose

How I can write a html code (templete) to a html file by php?


How I can write html code (not executable) to a file by using php filesystems. For example I want to put my html page template which includes DOCTYPE, HEADER, BODY BLOCK, FOOTER etc.


Solution

  • as i got you question

    save file index.php instead off index.html.

    inside you can use

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    </body>
    </html>
    

    when ever you need to write a php(inside body)

    use

    <?php
    //php code goes here
    ?>
    

    example

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <?php
    //php code goes here
    ?>
    </body>
    </html>
    

    php will work when file extension is .php. if file extension is .html browser will not recognize your PHP code and it shows complete code on browser