简体   繁体   English

我需要在包含的php文件中回显html吗

[英]Do I need to echo html inside included php file

I just learned how to include php .Here's the index or main php file 我刚刚学会了如何包含php。这是索引或主要的php文件

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

now in header.php file which way is better to print html 现在在header.php文件中哪种方式更好地打印html

Way 1 directly use html without php 方式1直接使用html而不使用php

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

Way 2 Using php and echo 方式2使用php和echo

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

Another quick question. 另一个快速问题。 Will it work if I use .html for the base or index file ?? 如果我使用.html作为基础或索引文件,它会工作吗? sorry for my bad english 对不起,我的英语不好

Directly use HTML without PHP: 在不使用PHP的情况下直接使用HTML:

<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. 您正在使用include()的文件必须具有.php扩展名,但是所包含的文件不一定需要.php扩展名。 The .html extension would work fine as well. .html扩展名也可以正常工作。

Just include the PHP file. 只需包含PHP文件。

BTW, in case you haven't, read about this too: 顺便说一句,如果您还没有的话,请阅读以下内容:

Difference between require, include and require_once? require,include和require_once之间的区别?

HTH. HTH。

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 : 或者,如果我有一个很好的块可以使用,也许在if语句中,我也喜欢这样:

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

The answer is sometimes different. 答案有时会有所不同。 If you use the same header on a lot of pages, use Way1. 如果您在许多页面上使用相同的标题,请使用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. 如果您只在一个地方进行此操作,则想使用Way 2(或将其保存在html中),因为对于研究页面的人而言,它一目了然。

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. 如果您更改服务器配置以告诉它首先查看它的PHP,您可以使用.html,但是您不应该这样做 ,它只会增加复杂性并且如果您做错了可能会产生意想不到的后果。

Use .php or .phtml for files with any amount php in it. 将.php或.phtml用于包含任意数量php的文件。 The only distinction you can make is to use .phtml files for files that are mostly html. 您可以做的唯一区别是将.phtml文件用于主要是html的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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