Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件\使用php将内容放入另一个文件的头部_Php_Html - Fatal编程技术网

文件\使用php将内容放入另一个文件的头部

文件\使用php将内容放入另一个文件的头部,php,html,Php,Html,因此,我正在创建一个用于其他用途的库,但是如何使文件中的内容具体地包含在或html标记属性等中呢 例如,这就是我想要做的 <html> <head> <?php include('content/starter/library.php'); ?> <!-- From that included file, theres a script that put content in the head.--> </head>

因此,我正在创建一个用于其他用途的库,但是如何使文件中的内容具体地包含在
html标记属性等中呢

例如,这就是我想要做的

<html>
<head>
    <?php include('content/starter/library.php'); ?>

    <!-- From that included file, theres a script that put content in the head.-->
</head>
<body>
    <!-- From that included file, theres a script that put content in the body -->
</body>
</html>

我只是想找到另一种方法,而不是为特定部分制作多个文件

<html>
<head>
    <?php include('content/starter/library_head.php'); ?>
</head>
<body>
    <?php include('content/starter/library_body.php'); ?>
</body>
</html>


我真的不想这么做。我对javascript不是很在行,所以,我不希望试图弄明白如何用javascript实现这一点。谢谢你以后的回答

如果您想使用一个文件(如您的问题所示),那么一种方法是在library.php文件中创建变量或函数,然后在模板中回显它们

// contents of the library.php file...
<?php
    $head_content = "put your <head> content here";
    $body_content = "put your <body> content here";
?>

// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
    <?php echo $head_content ?>
</head>
<body>
    <?php echo $body_content ?>
</body>
</html>
//library.php文件的内容。。。
//你的HTML文件。。。
更新

为了回答您评论中的问题,下面是一个使用函数的示例。您可以将所有代码放在一个函数中,然后在文档中的任何位置回显它

<?php
// contents of library.php...
function head() {
    $return  = '<link href="file.css" rel="stylesheet">';
    $return .= '<link href="another_file.css" rel="stylesheet">';

    return $return;
}

// your HTML file...
<html>
<head>
    <?php echo head(); ?>
</head>


PHP函数解释:

您应该将文件分开,这样每个页面上通常相同的页眉和页脚文件就可以包含在一行代码中。但是,你现在做这件事的方式,是大多数人做这件事的方式。但它们通常在头文件中包含头标记。然而,这在很大程度上取决于您的应用程序作为一个整体,因此人们很难告诉您最好在这里做什么。而且,正文不会是一个静态文件。它需要包含变量和函数来动态呈现html。这还取决于应用程序的类型。因为,在很多情况下,主体只会构建在每个页面上。我只使用一个php文件,并希望php的某些部分插入到包含php的另一个页面中。是否有其他方法使用变量来实现这一点。但是我有一个带有样式和div的php文件,是否有方法生成php节,而不是在中生成它们"或者“”,这个例子仍然在创建类似变量的东西。或者我们可以说,我想包含一个文件的特定部分,它是php,不能是变量。有可能在php中创建一个php函数吗?当函数被调用时,它将在其中执行php?这些都是部分。我正在做的正是您在问题中所要求的:一个包含要放入HTML页面的所有内容的文件。PHP函数中确实包含PHP,并将执行函数中的代码,以及其他函数中的代码。我建议您仔细阅读函数,看看它们能做些什么。:)