使用太多PHP包含一个坏主意? [英] Using too many PHP includes a bad idea?

查看:106
本文介绍了使用太多PHP包含一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,使用多个PHP includes()是否是一个坏主意。我问的唯一原因是因为我总是听到网站上有太多的样式表或脚本会产生更多的HTTP请求并减慢页面加载速度。我想知道关于PHP的问题。

My question is whether or not using multiple PHP includes() is a bad idea. The only reason I'm asking is because I always hear having too many stylesheets or scripts on a site creates more HTTP requests and slows page loading. I was wondering the same about PHP.

推荐答案

详细答案:

网页中引用的每个CSS或JS文件实际上都是由浏览器通过网络获取的,这通常涉及100毫秒或更长的网络延迟。对同一服务器的请求(按照惯例,虽然没有强制要求)一次一个或两个序列化,因此这些延迟堆积起来。

Every CSS or JS file referenced in a web page is actually fetched over the network by the browser, which involves often 100s of milliseconds or more of network latency. Requests to the same server are (by convention, though not mandated) serialized one or two at a time, so these delays stack up.

PHP包含文件,另一方面手,都在服务器本身处理。而不是100毫秒,本地磁盘访问将是10毫秒或更短,如果缓存,将是直接内存访问,这甚至更快。

PHP include files, on the other hand, are all processed on the server itself. Instead of 100s of milliseconds, the local disk access will be 10s of milliseconds or less, and if cached, will be direct memory accesses which is even faster.

如果你使用像 http://eaccelerator.net/ http://php.net/manual/en/book.apc.php 然后你的PHP代码将在服务器上预编译一次,然后它如果您将文件包含在一个地方或将它们全部放在一个地方,则无关紧要。

If you use something like http://eaccelerator.net/ or http://php.net/manual/en/book.apc.php then your PHP code will all be precompiled on the server once, and it doesn't even matter if you're including files or dumping them all in one place.

简答:

不要担心。对于这个问题,100次中有99次,更好的代码组织的好处超过了性能提升。

Don't worry about it. 99 times out of 100 with this issue, the benefits of better code organization outweigh the performance increases.

这篇关于使用太多PHP包含一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆