2

I just learned how to include php .Here's the index or main php file

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
    <?php include 'header.php'; ?>
</body>
</html>

now in header.php file which way is better to print html

Way 1 directly use html without php

<header>
<h1>Header</h1>
</header>

Way 2 Using php and echo

<?php
echo '
    <header>
    <h1>Header</h1>
    </header>
    '
?>

Another quick question. Will it work if I use .html for the base or index file ?? sorry for my bad english

UmeRonaldo
  • 619
  • 2
  • 7
  • 19
  • 1
    you need to use .php if you are going to be including a file like you are. You are better off if you can avoid using echo, more of a chance to forget a trailing " or something. – Jim Oct 30 '13 at 19:21
  • To help future requests, it is recommended that you accept an answer. Psst, little hint -- it gives you some reputation to accept an answer! – davewoodhall Jan 08 '14 at 21:40

5 Answers5

4

Directly use HTML without PHP:

<header>
<h1>Header</h1>
</header>

As for your second question:

The file you're using include() in must have .php extension, but the file that's being included doesn't necessarily need the .php extension. The .html extension would work fine as well.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
1

Just include the PHP file.

BTW, in case you haven't, read about this too:

Difference between require, include and require_once?

HTH.

Community
  • 1
  • 1
Portu
  • 542
  • 2
  • 4
  • 16
1

you should use include_once ;-)

http://us2.php.net/manual/en/function.include-once.php

kewl
  • 56
  • 10
1

There are a few ways to go around it. Rather than echoing everything, I like to go like this:

<title><?php echo $title; ?></title>

Or if I have a nice block to work with, maybe within an if statement, I also like to go like this :

<?php if($weather = "sunny") { ?>
    <div id="sunny">
        <p>It's a beautiful day outside.</p>
    </div>
<?php } // end if($weather = "sunny")
else { ?>
    <div id="sunny">
        <p>Today is yucky.</p>
    </div>
<?php } ?>
davewoodhall
  • 998
  • 3
  • 18
  • 43
0

The answer is sometimes different. If you use the same header on a lot of pages, use Way1. If you're only doing this in one place, you want to use Way 2 (or keep it in html), since it's more readable at quick glance to someone studying the page.

You can use .html for the file if you change the server config to tell it to look at it for php first, but you shouldn't do that, it just increases complexity and may have unintended consequences if you do it wrong.

Use .php or .phtml for files with any amount php in it. The only distinction you can make is to use .phtml files for files that are mostly html.

Vigilant
  • 235
  • 1
  • 9
  • `.phtml` was the standard file extension for PHP 2. Then `.php3` took over for the PHP 3 release. Since the release of PHP 4 it is only `.php.`. So not really needed to use `.phtml`. – Portu Oct 30 '13 at 19:26