PHP 许多包含文件
今天我读了一些关于将多个 JS 和 CSS 文件压缩为一个以降低带宽和 HTTP 请求的文章。 php 文件是否有类似的情况?创建包含少量 PHP 文件(header.php、footer.php 等)的母版页以获得格式良好且可读的代码是否比只创建一个大的 index.php 更好?
Today I read some articles about compressing multiple JS and CSS files to one in order to lower the bandwith and HTTP requests. Is there analogical situation with the php files? Is it better to create master pages that include few PHP files(header.php,footer.php and etc)in order to have a well formatted and readable code than having just a large index.php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 在服务器上运行,因此,仅将生成的 HTML 发送到客户端。为了清楚起见,将文件分开,这不会影响 HTTP 请求或带宽。
PHP is run on the server, and as such, only the resulting HTML is sent over to the client. Keep your files separated for clarity, it doesn't make a difference in the HTTP requests or bandwidth.
就像Xeon06提到的,PHP是一个服务器端脚本,并且不影响带宽,即通过HTTP传输的内容的大小。带宽大小基于 HTML、CSS、JS、图形、Flash 和任何客户端文件。由于浏览器会缓存此类文件,因此会减少带宽,因为浏览器将从缓存中获取文件内容,而不会再次从服务器下载。
如果我处于你的立场,我仍然会使用包含页眉、页脚或大多数页面中常见的内容。它只是使内容和代码的维护变得更容易。例如,如果您在所有页面上都有一个反馈表单,并且想要编辑字段名称、链接或其他内容,那么编辑一个文件会更容易,而不是通过您拥有的所有文件进行“查找和替换”,这是行不通的无论如何。
Like Xeon06 mentioned, PHP is a server-side script, and doesn't affect a bandwidth, which is size the of the content transferred through HTTP. Size of the bandwidth based on HTML, CSS, JS, Graphics, Flash and anything client-side files. Because browsers cache these kind of files, it reduces the bandwidth, because browser will get the file content from cache and not download from server again.
If I were in your shoes, I would still use includes for headers, footers or whatever is common in most pages. It just makes it easier to maintain a content and code. For example, if you have a feedback form on all pages and you want to edit a field name, link or something else, it's easier to edit one file instead of "Find and Replace" through all files you have, which doesn't work well anyways.