php optimization

高洛峰
Release: 2016-10-20 13:54:35
Original
939 people have browsed it

1 Quotes

About double quotes "", single quotes''
If you don't escape, use single quotes, because double quotes will search for variables, and single quotes only wrap strings

2 Arrays pushed onto the stack

$arr [] = 'x'; instead of array_push($arr, 'x'), there is no consumption of function calls

3 Multi-branch selection

switch case instead of multiple if else. By the way, when there is only one correct output, but There are many mistakes to be eliminated in the middle. Don’t use multiple if elses. You can do this, which will make the code more elegant

function demo(){
    if () return false;
    if () return false;
    if () return false;
    return 'x';
}
Copy after login

4 The choice of object-oriented and process-oriented

Things that can be simplified, try to simplify them as much as possible. Don’t try to build everything When making a class, the instantiation of the class consumes more memory than the function call. The instantiation of the class requires allocating heap memory, not stack memory

5 Try to use the functions that come with PHP

Don’t think of implementing it yourself, PHP The built-in functions are all written in C language. Can you write it in PHP more efficiently than in C?

6 Try not to use global variables

global, global variables affect the program structure,

7 This is PHP, Not c/c++

In c, i++ only requires one cpu instruction, so I always thought that $i++ was very efficient, but the frameworks I saw (such as tp) did not use $i++, but $i + = 1, I realized that I am too young, $i++ requires 4 opcodes in php

8 Memory leaks There are generally no memory leaks in php (it exits after running, there is no resident process), but I encountered it A program written by a colleague returned an empty page inexplicably, but the code logic seemed to be OK. I used xdebug to debug and found that the recursion has no exit, resulting in infinite calls, so the program may have a memory leak


Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!