Table of Contents
PHP中几个可以提高运行效率的代码写法、技巧分享,php写法
php 怎降低服务器消耗提高效率
在php中读取大文件,怎提高效率
Home php教程 php手册 PHP中几个可以提高运行效率的代码写法、技巧分享,php写法

PHP中几个可以提高运行效率的代码写法、技巧分享,php写法

Jun 13, 2016 am 09:26 AM
php Improve efficiency operating efficiency

PHP中几个可以提高运行效率的代码写法、技巧分享,php写法

废话不多说,直接看代码示例。

一、遍历数组

在遍历数组中注意count的使用次数,不要每次都去计算数组长度
效率慢的写法

复制代码 代码如下:


 
$array = array(1,2,3,4,5,6,7,8,9,10,....);
for($i=0;$k     echo $array[$i];
}
 
?>


效率快的写法

复制代码 代码如下:


 
$array = array(1,2,3,4,5,6,7,8,9,10,....);
for($i=0,$k     echo $array[$i];
}
 
?>

二、巧用函数

根据需要选择适用函数,如已知一个日期类型的时间'2012-06-04 10:43:00‘,只需要取得年月日。
效率慢的写法

复制代码 代码如下:


 
$date = '2012-06-04 10:43:00';
$arr = explode(' ',$date);
echo $arr[0];
 
?>


效率快的写法

复制代码 代码如下:


 
$date = '2012-06-04 10:43:00';
echo substr($date,0,10);
 
?>

三、单双引号

许多人误认为单引号与双引号一样使用,这是严重错误的。在PHP中单引号与双引号有着极大的区别,其中区别最大的一点在于双引号中能解析变量,单引号中不可以。也就由此产生了效率问题,单引号比双引号的效率要高
效率慢的写法

复制代码 代码如下:


 
// 效率低
$str = "一个变量值";
echo "这是一个双引号字符串{$str}";
 
?>


效率快的写法

复制代码 代码如下:


 
// 效率低
$str = '一个变量值';
echo '这是一个双引号字符串'.$str;
 
?>

四、尽量简洁

直接看代码
普通写法

复制代码 代码如下:


 
function cheng($a,$b){
    $c = $a*$b;
    return $c;
}
 
$result = cheng(10,16);
echo $result;
 
?>


简洁写法

复制代码 代码如下:


 
function cheng($a,$b){
    return $a*$b;
}
 
echo cheng(10,16);
 
?>

五、分支妙用

如果分支过多,就用switch。如果很少,就用ifelse
效率慢的写法

复制代码 代码如下:


 
if($a == 1){
    // 代码块 
}elseif($a == 2){
    // 代码块
}elseif($a == 3){
    // 代码块
}elseif($a == 4){
    // 代码块
}elseif($a == 5){
    // 代码块
} ...
 
?>


效率高的写法

复制代码 代码如下:


 
switch($a){
    case 1:
        // 代码块1
        break;
    case 2:
        // 代码块2
        break;
    case 3:
        // 代码块3
        break;
        ...
    default:
        // 默认块
}
 
?>


 
PHP做为服务器端语言,程序设计尤为重要,保持高效的风格会让你的程序运行的更顺畅!

php 怎降低服务器消耗提高效率

优化PHP的方法很多啊,可以从服务器、服务器软件如APACHE、数据库如MYSQL上下功夫,但最重要的还是在PHP代码上下功夫,改更快的算法、减少运算等等。
下面是摘录的:
1.如果一个方法可静态化,就对它做静态声明。速率可提升至4倍。

2.echo 比 print 快。

3.使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接。

4.在执行for循环之前确定最大循环数,不要每循环一次都计算最大值。

5.注销那些不用的变量尤其是大数组,以便释放内存。

6.尽量避免使用__get,__set,__autoload。

7.require_once()代价昂贵。

8.在包含文件时使用完整路径,解析操作系统路径所需的时间会更少。

9.如果你想知道脚本开始执行(译注:即服务器端收到客户端请求)的时刻,使用$_SERVER[‘REQUEST_TIME’]要好于time()。

10.函数代替正则表达式完成相同功能。

11.str_replace函数比preg_replace函数快,但strtr函数的效率是str_replace函数的四倍。

12.如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行代码接受数组作为查询和替换的参数。

13.使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。

14.用@屏蔽错误消息的做法非常低效。

15.打开apache的mod_deflate模块。

16.数据库连接当使用完毕时应关掉。

17.$row[‘id’]的效率是$row[id]的7倍。

18.错误消息代价昂贵。

19.尽量不要在for循环中使用函数,比如for ($x=0; $x
20.在方法中递增局部变量,速度是最快的。几乎与在函数中调用局部变量的速度相当。

21.递增一个全局变量要比递增一个局部变量慢2倍。

22.递增一个对象属性(如:$this->prop++)要比递增一个局部变量慢3倍。

23.递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。

24.仅定义一个局部变量而没在函数中调用它,同样会减慢速度(其程度相当于递增一个局部变量)。PHP大概会检查看是否存在全局变量。

25.方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都)添加了10个方法,但性能上没有变化。

26.派生类中的方法运行起来要快于在基类中定义的同样的方法。

27.调用带有一个参数的空函数,其花费的时间相当于执行7至8次的局部变量递增操作。类似的方法调用所花费的时间接近于15次的局部变量递增操作。

28.用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会。当然,只有当你不需要在字符串中包含变量时才可以这么做。

29.输出多个字符串时,用逗号代替句点来分隔字符串,速度更快。注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。

30.Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面,少用脚本。

31.除非脚本可以缓存,否则每次调用时都会重新编译一次。引入一套PHP缓......余下全文>>
 

在php中读取大文件,怎提高效率

主要看你是怎么用,如果仅是读出来,再写到另外一个文件中去,速度不会太慢。
给个演示地址你看看:
service.020i.cn/baidu/bigfile.php
 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles