PHP用头文件头和jQuery编写新的PHP文件吗?

时间:2019-03-17 14:10:07

标签: php jquery html

嗨,我尝试用php编写其他php文件,但始终失败... 是否可以像我的代码片段一样来做? 预先感谢。

$newfile = fopen("file.php", "w") or die("Unable to open file!");
$txt = "<html>\n
<title>my php file written with php</title>\n
<link href="css/style.css" rel="stylesheet">\n
include 'header.php'\n
<BR><BR><BR><BR>\n
This is the php file written by a php page.\n\n
Here is a link: <a HREF='index.html'>home</a>.\n
<script>
$(document).on('click', '.option-btn', function () {
	$(this).toggleClass('open');
	$('.control-center').toggleClass('open');
});
</script>\n
</body>\n
</html>";
fwrite($newfile, $txt);
fclose($newfile);

1 个答案:

答案 0 :(得分:1)

您尝试将双引号字符串放在双引号字符串中,因此会出错。

在这一行

<link href="css/style.css" rel="stylesheet">\n

在这一行中,您错过了;,尽管这并没有引起错误。

 include 'header.php';\n

如果要运行包含,则需要将其包装在<?php ... ?>

您错过了<head>标签和<body>标签,而<script>应该放在<head>部分。

修改的代码

$newfile = fopen("file.php", "w") or die("Unable to open file!");
$txt = "<html>\n
<head>
<title>my php file written with php</title>\n
<link href='css/style.css' rel='stylesheet'>\n
<script>
$(document).on('click', '.option-btn', function () {
    $(this).toggleClass('open');
    $('.control-center').toggleClass('open');
});
</script>\n
</head>\n
<?php
include 'header.php';
?>
<body>
<BR><BR><BR><BR>\n
This is the php file written by a php page.\n\n
Here is a link: <a HREF='index.html'>home</a>.\n

</body>\n
</html>";
fwrite($newfile, $txt);
fclose($newfile);