PHP性能考虑? [英] PHP performance considerations?

查看:44
本文介绍了PHP性能考虑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 PHP 站点,但目前我使用的唯一 PHP 是某些页面上的六个左右的包含.(我可能最终会使用一些数据库查询.)

I'm building a PHP site, but for now the only PHP I'm using is a half-dozen or so includes on certain pages. (I will probably use some database queries eventually.)

与静态 HTML 相比,简单的 include() 语句是否会影响速度或缩放?什么样的事情会导致网站陷入困境?

Are simple include() statements a concern for speed or scaling, as opposed to static HTML? What kinds of things tend to cause a site to bog down?

推荐答案

严格来说,直接 HTML 总是比服务器端方法提供更快的服务,因为服务器不必对代码进行任何解释.

Strictly speaking, straight HTML will always serve faster than a server-side approach since the server doesn't have to do any interpretation of the code.

为了回答更大的问题,有很多因素会导致您的网站陷入困境;与 PHP 相比,您的代码何时导致问题并没有特定的阈值.(请记住,雅虎的许多网站都是由 PHP 驱动的,所以不要认为 PHP 无法扩展).

To answer the bigger question, there are a number of things that will cause your site to bog down; there's just no specific threshold for when your code is causing the problem vs. PHP. (keep in mind that many of Yahoo's sites are PHP-driven, so don't think that PHP can't scale).

我注意到的一件事是,最慢的 PHP 驱动站点包含的内容超出了显示特定页面所需的内容.OSCommerce (oscommerce.com) 是最受欢迎的 PHP 驱动的购物车之一.然而,它有一个坏习惯,就是在每个页面上都包含所有核心功能(以防万一).因此,即使您不需要显示信息框",也会加载该功能.另一方面,有许多 PHP 框架(例如 CakePHP、Symfony 和 CodeIgniter)采用按需加载"方法.

One thing I've noticed is that the PHP-driven sites that are the slowest are the ones that include more than is necessary to display a specific page. OSCommerce (oscommerce.com) is one of the most popular PHP-driven shopping carts. It has a bad habit, however, of including all of their core functionality (just in case it's needed) on every single page. So even if you don't need to display an 'info box', the function is loaded. On the other hand, there are many PHP frameworks out there (such as CakePHP, Symfony, and CodeIgniter) that take a 'load it as you need it' approach.

我的建议如下:

  1. 不要包含超过特定页面所需的功能
  2. 保持基本功能独立(尽可能使用 MVC 方法)
  3. 如果您认为将嵌套包含(例如,页面 A 包含包含文件 C 的文件 B),请使用 require_once 而不是包含.这将避免多次包含同一个文件.如果找不到文件,它也会停止进程;从而帮助您进行故障排除过程;)
  4. 如果可能,将静态页面缓存为 HTML - 避免在事情没有改变时重新解析

这篇关于PHP性能考虑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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