php 会增加页面加载时间吗?

发布于 2024-11-28 16:55:44 字数 928 浏览 2 评论 0原文

我在我的 网站 上使用了 php,因此它可以显示多种语言。我注意到 html 文件的大小减小了,因为大部分文本被移动到一个新的大型 php 文件中,其中包含所有可用语言的所有“惯用”文本(在我的例子中只有两种)。

我的问题是:以前下载的 html 页面只包含必要的文本。现在下载了一个较小的 html 页面,但总是包含一个大的 php 文件,无论 html 使用更多还是更少的行。

问题:

  1. 当包含 php 文件时(include_once(file.php);require_once(file.php);)会发生什么? file.php 的内容是否被复制到 html 中?它只是用来对语言处理器说“如果您需要解析名称,您可能需要查看file.php”吗?
  2. 什么更(速度)高效;拥有一个大型 php,其中包含每个 html 页面的每种语言; 每个html页面有一个php文件,仅包含该页面所需的代码?
  3. 是否应该在使用之前包含 php, 应该是页面顶部的常规包含,以获取最终将在 html 页面中使用的大部分 php?
  4. 最后,php 会增加页面加载时间吗?

PS:有人尝试过 PHP Speedy 吗?真的有效吗?它有什么问题(兼容性)吗?


我发现:(

  1. 检查解决方案)
  2. 两种解决方案具有完全相同的性能。我选择按语言分隔文件,因为这样感觉更整洁。
  3. 与上面相同,对性能没有影响。

I used php on my website so it can display multiple languages. I noticed the size of the html files reduced because most of the text was moved to a new large php file, which contains all "idiomatic" text in all languages available (just two in my case).

My problem is: Previously a html page was downloaded with just the necessary text. Now a smaller html page is downloaded but a large php file is always included, whether the html uses more or less lines of it.

Questions:

  1. When a php file is included (include_once(file.php); or require_once(file.php);) what is happening? Is the content of the file.php being copied to the html? Is it just used to say to the language processor "If you need to resolve a name, you may want to look at file.php"?
  2. What is more (speed) efficient; to have one large php with every language of every html page inside; or to have one php file per html page only with the code needed by that page?
  3. Should a php be included right before being used, or should be a general include at the top of the page that gets most of the php that will eventually be used along the html page?
  4. Finally, does php increase a page loading time?

P.S: Anyone tried PHP Speedy? Does it really works? Does it have any problems (compatibility) ?


I found out:

  1. (check solution)
  2. Both solutions have exactly the same performance. I chose to separate the file by language because it feels tidier.
  3. Same as above, no impact on performance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

中二柚 2024-12-05 16:55:44

1) 每个包含/需要语句都被包含/需要文件的内容替换。包含的文件本身的内容正在成为执行脚本的一部分,但是它是否会成为 HTML 响应的一部分取决于内容本身。

2) 除非您有大量访问者,否则您几乎不会注意到包含一个大文件和几个较小文件之间的区别。然而,最好的做法是仅包含实际需要的部分,因为这样可以节省 Web 服务器内存和资源,这在出现大量请求时可能会出现问题。

3) 在哪里包含代码并不重要。出于纯粹的组织原因,最好将所有内容包含在脚本的开头,但有时这是不可能的,这意味着您经常会看到包含在函数等内部的代码。

4) 解析 PHP 文件时会出现一些延迟与纯 HTML 文件相比,因为该过程涉及到 Web 服务器扩展本身的“往返”,但是由于内部缓存机制等原因,延迟很难发现,甚至几乎不可能通过统计来证明。

编辑:如果您担心网站性能,可以从一些 Google Chrome 扩展程序开始,例如 页面速度速度追踪器。它们将帮助您查明可能降低网站性能的常见问题。

对于服务器端,最好使用代码分析器来查找潜在的性能消耗,例如大量函数调用等。 为此,您可以使用 xDebug PHP 调试器,配有出色的分析器和 WebGrind到分析性能本身。

1) Every include / require statement is replaced by the content of the included / required file. The content of the included file itself is becoming part of the executing script however if it will become part of the HTML response depends upon the content itself.

2) Unless you have a very large number of visitors you will hardly notice a difference between including one large file and several smaller ones. However its good practice to include only parts which are actually required because that way you are saving web server memory and resources which might present a problem in case of a large number of requests.

3) It doesn't really matter where you include your code. For pure organization reasons its good to include all the stuff right at the beginning of the script however sometimes this is not possible which means that you'll often see code included inside functions etc.

4) There is some latency involved with parsing PHP files when compared against pure HTML files because the process involves a "roundtrip" to the web server extension itself however because of the internal caching mechanisms etc. that latency is very hard to spot, if not almost impossible to prove statistically.

EDIT: If you are concerned about your web site performance a good place to start are some of the Google Chrome extensions such as Page Speed and Speed Tracer. They will help you pinpoint the usual problems that might lower your web site performance.

For the server side its always good to use code profilers to find potential performance hogs such as high number of function calls etc. For that you can use xDebug PHP debugger which comes with an excellent profiler and WebGrind to analyze the performance itself.

柒七 2024-12-05 16:55:44

以前下载的 html 页面仅包含必要的文本。
现在下载了一个较小的 html 页面,但总是下载一个大的 php 文件
包括,html 是否使用了更多或更少的行。

我不喜欢这种方法。为什么要加载不必要的东西?另外,随着站点的扩展,它可能会成为一个非常臃肿的文件。

还有其他方法,例如:

1)从数据库中获取文本。

2) 根据语言将文本存储在单独的文件中。即使您是 php 新手,您也可以轻松完成此操作。它应该比您当前的解决方案更快。

关于你的问题:

当包含 php 文件时 (include_once(file.php); 或

require_once(file.php);) 发生了什么?内容是
file.php被复制到html?难道只是用来对
语言处理器“如果您需要解析名称,您可能需要
看看file.php”?

当你包含或需要一个文件时(并且如果它成功加载),
服务器将执行您其中的任何代码。

这是一个非常广泛的问题,需要更详细的答案,请查阅一些文献。

应该在使用之前包含 php,还是应该是

一般包含在页面顶部,获取大部分 php
最终会在html页面中使用吗?

两种方法都是有效的并且适合不同的情况。有时,您需要在发生其他事情之前执行一些代码,因此需要包含一些代码(在您的情况下,可能是识别应该使用哪种语言)。其他时候您可能想包含页脚,因此只有将其包含在底部的某个位置才有意义。

最后,php会增加页面加载时间吗?

要看情况,你自己衡量一下。但没有理由远离 php。

Previously a html page was downloaded with just the necessary text.
Now a smaller html page is downloaded but a large php file is always
included, whether the html uses more or less lines of it.

I don't like this approach. Why load unnecessary stuff? Plus as your site expands, it can become a very bloated file.

There are other ways, like:

1) Fetching text from database.

2) Storing text in separate files depending on language. This one you can easily do even if you're new to php. And it should be faster than your current solution.

On your questions:

When a php file is included (include_once(file.php); or

require_once(file.php);) what is happening? Is the content of the
file.php being copied to the html? Is it just used to say to the
language processor "If you need to resolve a name, you may want to
look at file.php"?

When you include or require a file (and if it successfully loads),
server will execute whatever code you have in it.

It's a very broad question for more detailed answer, check out some literature.

Should a php be included right before being used, or should be a

general include at the top of the page that gets most of the php that
will eventually be used along the html page?

Both approaches are valid and appropriate for different situations. Sometimes you need some code executed, and hence included, before anything else happens (in your case it might be recognition of which language should be used). Other times you might wanna include footer, so it only makes sense including it somewhere on the bottom.

Finally, does php increase a page loading time?

Depends on situation, you'd have to measure yourself. But it's no reason to stay away from php.

孤独岁月 2024-12-05 16:55:44

这一切都取决于。按顺序来说,

  1. 不。当您说 include 时,PHP 会执行 file.php 中的所有操作,然后继续执行其中包含 include 的操作。
  2. 这取决于。我认为 1 页/语言设置会慢一些。
  3. 没有什么区别。我通常把它们放在顶部,因为所有东西都在同一个地方。
  4. 可能有,但我不这么认为。

不,PHP Speedy 负责缩小和合并您的 JavaScript 和 CSS。缩小意味着:

div#content {
    color: #000;
}

变成这样:

div#content{color:#000;}

It all depends. In order,

  1. No. When you say include, PHP does EVERYTHING in file.php and then continues with the one that had include in it.
  2. It depends. I think that the 1 page/language setup would be a little bit slower.
  3. There's no difference. I usually put them at the top, because everything is in the same place.
  4. It might, but I don't think so.

And no, PHP Speedy is responsible for minifying and merging your JavaScript and CSS. Minifying means this:

div#content {
    color: #000;
}

becomes this:

div#content{color:#000;}
尽揽少女心 2024-12-05 16:55:44

是的,当您使用 PHP 时,请求会花费更长的时间,但较小的请求根本不会花费太多时间。从 PHP 到 HTML,差异并不明显。

当您包含某些内容时,PHP 会将其解析为 php 文件(并且需要为每个包含从 HDD 加载文件)。

Yes, when you use PHP it takes longer for requests, but smaller requests don't take much time at all. the difference isn't really noticeable from PHP to HTML.

When you include something, PHP is parsing it as if it were a php file (and that requires loading the file from the HDD for every include).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文