I've been coding in PHP ever since I was 13, over the years I have seen many different coding styles and standards being used. However most of them weren't optimised.

我从13岁就开始编写PHP代码,这些年里我见过很多不同风格不同标准的代码。然而大多时候他们还没有进行过优化。

This time I'd like to talk about different ways on how you can speed up your scripts by optimizing your code to reduce your server load.
这次我想聊聊如何让你的代码更优化,以降低服务器的负载。

Coding tips

编码要点


Quotes

引号

Try using single quotes as much as possible, it's faster than double quotes because PHP searches for variables in text surrounded in double quotes.

尽可能的使用单引号,它比双引号快,因为PHP会在双引号包围的字符串中搜索变量。

Using single quotes in arrays is also recommended since it's faster than calling it with double or no quotes.

在数组中也推荐使用单引号,因为它要比双引号和不加引号快。

Echo VS. print

Echo 与 print

Echo is faster than print, if you're using concatenation in your echo command then you could optimise it further by using multiple parameters instead of concatenation.

Echo 比 print 快,尽可能的使用单引号,如果你在echo 命令中要用到连接,那就把它优化成多个参数相连。

The print function can't handle multiple parameters so don't even try.

print函数不能处理多个参数,所以,请别尝试。

$name = 'zenk0';echo 'the user ', $name, ' has been selected for a special event.';//slower and more widely usedecho 'The user ' . $name . ' has been selected for a special event.';

For loops循环

Define your count variable before you start looping instead of in your loop. If you don't do this then your count function will be repeated everytime a loop happens.不要在循环体内,而是在循环体外定义 count 变量。如果不这样,你的计数函数每次循环都会调用。

$array = array('one', 'two', 'three');$count = count($array);//slow : for($i=0; $i < count($array); $i++)for($i=0; $i < $count; $i++){echo array[$i];}

Simple conditionals简单条件判断

It's better to use a switch statement instead of if/else if/else when you're using simple conditionals.如果你的条件比较简单,使用 switch 比用  if/else if/else 好多了。

Includes & requiresIncludes 和 requires

There are several ways to gain some speed in this are; first of all drop the use of require_once it's much slower than include_once. Try to use full paths in your includes and requires, it'll spare the server some time on resolving the paths.这儿有几种方法可以加速: 首先,用require_once比include_once慢多了。尽量在你的包含和引用中用全路径,这样可以省掉解析路径的开销。

There are 3 ways of doing this; You could just simply set the include path.有三种方式可以用;你只要简单的设置一下include的路径。

// Works as of PHP 4.3.0set_include_path('/inc');

// Works in all PHP versionsini_set('include_path', '/inc');

Or you could create a variable with the include path and concatenate it at the start of an include.或者新建个用于包含的路径变量,并把它放到include连接起来。

$include = '/home/user/www';include ($include . '/library/loader.php');

As a third example; you could also just get the directory path from the current file.第三个例子:你也可以只获取当前文件的路径。

$dir = dirname(__FILE__);include ($dir . '/library/loader.php');

Handling strings处理字符串

Try to make use of as much str_ functions as possible instead of turning to preg_ functions.尽量使用 str_ 函数代替 preg_ 函数。

Str_replace is much faster than preg_replace if you're not making use of regex patterns. In turn strstr is faster than using str_replace.要是你不用正则表达式,str_replace比preg_replace快多了。 但是strstr 比 str_replace快。

Try to make use of these functions instead of preg_ functions: strpos, strpbrk, strncasecmp.尽量用这些函数来代替preg_函数: strpos, strpbrk, strncasecmp。

If you need to check  if a string has a certain length it's better to use the isset trick than using strlen.如果你需要检查一个字符串的长度,巧妙的使用isset比strlen好多了。

$str = 'have a nice day';//check if the string has more than 6 characters//slow, checks if the string has less than 7 characters.if(strlen($str) < 7)//fast, if there is no seventh character setif(!isset($str{6}))

This is because isset is a language construct, whereas strlen is a function that is looked up.这是因为,isset是一种语言结构,而strlen是一个函数。

Ofcourse there are several tools than can help you; use a code profiler.当然有一些工具可以帮忙:用 a code profiler。

They will tell you which how much time is spent parsing your script and which part requires the most time.  This makes it easy to find bottlenecks in your code.他会告诉你脚本运行的耗时分析和哪个部分用了最多的时间。这让你很简单就能找到你代码中的瓶颈。

[译]Speeding up your PHP scripts相关推荐

  1. RUST直接升钢指令_[译]参照TypeScript学习Rust-part-1

    [译]参照TypeScript学习Rust-1 · 前端在线​regx.vip 对于前端,笔者比较认可Rust作为前端开发技术栈投资的,本文系列翻译旨在分享.学习Rust这门语言. Rust常常被认为 ...

  2. Linux的shell scripts

    一.什么是脚本(scripts) 安装一定逻辑关系记录的命令文件,在此文件有可执行权限的情况下可以用文件名称发起脚本内记录命令的执行,shell脚本是一种解释性语言,文件内记录的动作需要解释器shel ...

  3. Matplotlib Tutorial(译)

    Matplotlib Tutorial(译) 翻译自:Matplotlib tutorialNicolas P. Rougier - Euroscipy 2012 toc {: toc} 这个教程基于 ...

  4. 手把手教创建你的第一个以太智能合约:ETHEREUM PET SHOP(译)

    手把手教创建你的第一个以太智能合约:ETHEREUM PET SHOP(译) 原文地址 : http://truffleframework.com/tutorials/pet-shop 译者:luci ...

  5. React.js开发生态系统概览 [译-转]

    React.js 开发生态系统概览 [译] JavaScript领域发展速度很快,甚至有人认为这已经引起了负效应.一个前端库从早期开发的小玩具,到流行,再到过时,可能也就几个月时间.判断一个工具能否在 ...

  6. [译]机器人操作系统简介:终极机器人应用框架(上)

    2019独角兽企业重金招聘Python工程师标准>>> [译]机器人操作系统简介:终极机器人应用框架 /*** 原文出处:https://www.toptal.com/robotic ...

  7. HTML5 Canvas和EaselJS入门(译)

    HTML5中最受开发者期待的一项新特性莫过于Canvas(画布)元素了.Canvas元素提供了一个可以动态渲染图形和位图的位图画布.它非常类似于Flash中的Bitmap和BitmapData两个类. ...

  8. Facebook 开源 M2M-100,不依赖英语互译百种语言

    Facebook 10 月 19 日开源了 M2M-100 模型的源代码,并称该算法是第一个能够在不依赖英语数据的情况下,在 100 种语言之间进行翻译的算法.也就是这说,无需通过英文做中介,该模型就 ...

  9. 服务器webpack构建性能,[译] 优化 WEBPACK 以更快地构建 REACT

    如果您的 Webpack 构建缓慢且有大量的库 -- 别担心,有一种方法可以提高增量构建的速度!Webpack 的 DLLPlugin 允许您将所有的依赖项构建到一个文件中.这是一个取代分块的很好选择 ...

最新文章

  1. 在直播问题上,智能电视们不应该沉默
  2. [ python ] 正则表达式及re模块
  3. 坑 之 tensorflow使用sess.run处理图片时越来越慢,占用内存越来越大的问题
  4. 音视频技术开发周刊 93期
  5. PP视频怎么查看云钻的兑换记录呢
  6. 【vue开发问题-解决方法】(八)利用axios拦截器实现elementUI中加载动画,控制加载区域
  7. openstack network
  8. ubuntu之更新cmake版本
  9. DXUT框架剖析(4)
  10. python 开源项目大全
  11. 使用dumpbin查看dll有哪些函数
  12. 【Android实战】----从Retrofit源码分析到Java网络编程以及HTTP权威指南想到的
  13. 爱了!再来推荐5个Java项目开发快速开发脚手架。项目经验和私活都不愁了~
  14. java 排序返回索引_java数组排序和索引
  15. Android NDK开发,使用ndk-build编译,写的太详细了
  16. ensp的下载与安装
  17. 运维工程师具备的基本技能
  18. “二选一”是支付宝首创,背后是帝国的霸权逻辑
  19. Hadoop MapReduce Splits 切片源码分析及切片机制
  20. python蟒蛇画法

热门文章

  1. 报告解读丨基于消费者洞察的鞋服品牌数字化营销新思路
  2. JMS学习(六)--提高非持久订阅者的可靠性 以及 订阅恢复策略
  3. 看我如何跨虚拟机实现Row Hammer攻击和权限提升
  4. jquery和zepto冲突解决以及体会
  5. jQuery判断checkbox是否选中的3种方法
  6. android(cm11)状态栏源码分析(一)
  7. centos7 安装 oracle 11G
  8. Android客户端实现七牛云存储文件上传
  9. Tips - C++
  10. 用到的oracle sql语句-001