40個優化php代碼的技巧-PHP編程教程

May 9th, 2011

http://www.huomo.cn/developer/article-58c.html

来源:艺酷建站

40个优化php代码的技巧

1.如果一个方法能被静态,那就声明他为静态的,速度可提高1/4;

2.echo的效率高于print,因为echo没有返回值,print返回一个整型;

3.在循环之前设置循环的最大次数,而非在在循环中;

4.销毁变量去释放内存,特别是大的数组;

5.避免使用像__get, __set, __autoload等魔术方法;

6.requiere_once()比较耗资源;

7.在includes和requires中使用绝对路径,这样在分析路径花的时间更少;

8.如果你需要得sexinsex到脚本执行时的时间,$_SERVER[‘REQUSET_TIME’]优于time();

9.能使用字符处理函数的,尽量用他们,因为效率高于正则;

10.str_replace字符替换比正则替换preg_replace快,但strtr比str_replace又快1/4;

11.如果一个函数既能接受数组又能接受简单字符做为参数,例如字符替换,并且参数列表不是太长,可以考虑多用一些简洁的替换语句,一次只替换一个字符,而不是接受数组做为查找和替换参数。大事化小,1 1>2;

12.用@掩盖错误会降低脚本运行速度;

13.$row[‘id’]比$row[id]速度快7倍,建议养成数组键加引号的习惯;

14.错误信息很有用;

15.在循环里别用函数,例如For($x=0; $x < count($array); $x), count()函数在外面先计算;

16.在方法里建立局部变量速度最快,几乎和在方法里调用局部变量一样快;

17.建立一个全局变量要比局部变量要慢2倍;

18.建立一个对象属性(类里面的变量)例如($this->prop )比局部变量要慢3倍;

19.建立一个未声明的局部变量要比一个初始化的局部变量慢9-10倍;

20.声明一个未被任何一个函数使用过的全局变量也会使性能降低(和声明相同数量的局部变量一样),PHP可能去检查这个全局变量是否存在;

21.方法的性能和在一个类里面定义的方法的数目没有关系,因为我添加10个或多个方法到测试的类里面(这些方法在测试方法的前后)后性能没什么差异;

22.在子类里方法的性能优于在基类中;

23.只调用一个参数并且函数体为空的函数运行花费的时间等于7-8次$localvar 运算,而一个类似的方法(类里的函数)运行等于大约15次$localvar 运算;

24.Surrounding your string by ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string.

25.当输出字符串时用逗号代替点分割更快些。注意:这只对echo起作用,这个函数能接受一些字符串作为参数;

26.在apache服务器里一个php脚本页面比相应的HTML静态页面生成至少要多花2-10倍的时间,建议多用些静态HTML页面和少量的脚步;

27.除非你的安装了缓存,不然你的php脚本每次被访问都需要被重编译。建议安装个php缓存程序,这样通过去除一些重复的编译来很明显的提高你20-100%的性能;

28.建议用memcached,高性能的分布式内存对象缓存系统,提高动态网络应用程序性能,减轻数据库的负担;

29.使用ip2long()和long2ip()函数把IP地址转成整型存放进数据库而非字符型。这几乎能降低1/4的存储空间。同时可以很容易对地址进行排序和快速查找;

30.使用checkdnsrr()通过域名存在性来确认部分email地址的有效性,这个内置函数能保证每一个的域名对应一个IP地址;

31.如果你在使用php5和mysql4.1以上的版本,考虑使用mysql_*的改良函数mysqli_*;

32.试着喜欢使用三元运算符(?:);

33.在你想在彻底重做你的项目前,看看PEAR有没有你需要的。PEAR是个巨大的资源库,很多php开发者都知道;

34.使用highlight_file()能自动打印一份很好格式化的页面源代码的副本;

35.使用error_reporting(0)函数来预防潜在的敏感信息显示给用户。理想的错误报告应该被完全禁用在php.ini文件里。可是 如果你在用一个共享的虚拟主机,php.ini你不能修改,那么你最好添加error_reporting(0)函数,放在每个脚本文件的第一行(或用 require_once()来加载)这能有效的保护敏感的SQL查询和路径在出错时不被显示;

36.使用 gzcompress() 和gzuncompress()对容量大的字符串进行压缩(解压)在存进(取出)数据库时。这种内置的函数使用gzip算法能压缩到90%;

37.通过参数变量地址得引用来使一个函数有多个返回值。你可以在变量前加个“&”来表示按地址传递而非按值传递;

38.Fully understand “magic quotes” and the dangers of SQL injection. I’m hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it’s absolutely critical to understand. If you’ve never heard the term before, spend the entire rest of the day googling and reading.

39.使用strlen()因为要调用一些其他操作例如lowercase和hash表查询所以速度不是太好,我们可以用isset()来实现相似的功能,isset()速度优于strlen();

40.When incrementing or decrementing the value of the variable $i happens to be a tad slower then $i. This is something PHP specific and does not apply to other languages, so don’t go modifying your C or Java code thinking it’ll suddenly become faster, it won’t. $i happens to be faster in PHP because instead of 4 opcodes used for $i you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend’s PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.

PHP, Program

PHP Download File in IE

October 29th, 2010

<?php

$filename = ‘file.txt’;
$contents = “”;

header(“Content-type: application/octet-stream”);
header(“Content-Type: application/force-download”);
header(“Content-Description: File Transfer”);
header(“Content-Type: application/download”);
header(‘Content-Disposition: attachment; filename=’.$filename);

$contents .= ‘Test Download File’;

echo $content;

?>

IE 不可以直接打網址(HTTP://WWW.XXX.COM/download.php)去下載file, 只能用link <a href=”./download.php”>Download</a>

PHP , ,

Database – test auto_increment

September 28th, 2010

CREATE TABLE `test` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY

) ENGINE = MYISAM

今次 測試insert情況,

INSERT INTO `test2` VALUES (0)

在測試所見, 如果id 是零, 它會在最後insert

第2次INSERT 我嘗試 -1

INSERT INTO `test2` VALUES (-1)

結果是順利INSERT -1
第3次INSERT我再嘗試零

INSERT INTO `test2` VALUES (0)

結果也一樣, 在最後出現

(雖然這個測試好似好淺, 但這個是非常重要..)
Important: 在傳送資料(在一個舊table資料傳送一個新table), 如果有id 是零是非常嚴重

例如:
CREATE TABLE `test`.`test2` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`data` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL

) ENGINE = MYISAM

CREATE TABLE `test`.`test3` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`data` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL

) ENGINE = MYISAM

舊table

id data
0 abc
1 def
2 ghi

INSERT IGNORE INTO test3
SELECT *
FROM test2

當資料傳送後, 新table的情況,

id data
1 abc
2 ghi

在測試時,  其實insert不一定是跟id 去insert
有可能insert的次序是有機會亂(也可能是 1, 0,2)

如果id是 fk 那資料就全錯了

CREATE TABLE `test`.`test2` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`data` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL

) ENGINE = MYISAM

MYSQL ,

上一頁 網頁地址

September 16th, 2010

Javascript

<INPUT TYPE=”button” VALUE=”上一頁  ” onClick=”history.go(-1)”>
<INPUT TYPE=”button” VALUE=”下一頁” onCLick=”history.go(1)”>

PHP

$_SERVER[HTTP_REFERER] //可以得到上一頁的地址
$_SERVER[PHP_SELF] //得到當前頁面地址

$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’]

$_SERVER[HTTP_HOST].$_SERVER[PHP_SELF].”?”.$_SERVER[QUERY_STRING]

如果網頁做左一些action(例如: update資料, 之後回到同一頁), 有一些browers按一次還是同一頁, 要第2下才會回到上一頁, 我的解決:

$pos = strpos($_SERVER[‘HTTP_REFERER’], $_SERVER[“PHP_SELF”]);

if($pos === FALSE)
{
setcookie(‘back_href’, $_SERVER[‘HTTP_REFERER’]);
}

if($_COOKIE[‘back_href’])
$back_href = $_COOKIE[back_href];
else
$back_href = $_SERVER[HTTP_REFERER];

if($back_href)
{
echo ‘<a href=”‘.$back_href.'”>Back</a>’;
}

HTML, Javascript, PHP, Program ,

PHP5 時間相差8小時問題

September 7th, 2010

找到php.ini中的“;date.timezone =”這行,將“;”去掉,改成“date.timezone = PRC”(PRC:People’s Republic of China 中華人民共和国),重新開Apache,問题解决。

Apache , ,

IE bug – Colspan Bug in IE

August 28th, 2010

<table border=”1″ width=”100%”>
<tr>
<th align=”center” width=”1″><input type=”checkbox” value=”2″ /></th>
<th>Name 1</th>
<th>Name 2</th>
<th>Name 3</th>
</tr>
<tr>
<td colspan=”4″>
<input type=”button” name=”it is button 1″ value=”it is button Submit 1″ />
<input type=”button” name=”it is button 2″ value=”it is button Submit 2″ />
</td>
</tr>
</table>

My Solution :

<table border=”1″ width=”100%”>
<tr>
<th align=”center” width=”1″><input type=”checkbox” value=”2″ /></th>
<th>Name 1</th>
<th>Name 2</th>
<th>Name 3</th>
</tr>
<tr>
<td colspan=”4″ style=”width:auto;”>
<input type=”button” name=”it is button 1″ value=”it is button Submit 1″ />
<input type=”button” name=”it is button 2″ value=”it is button Submit 2″ />
</td>
</tr>
</table>

CSS, HTML

PHP – 查這個function在那裡使用

August 28th, 2010

查這個function a 那裡會被使用

function a(){

echo ‘<pre>’;

print_r(debug_backtrace(true));

echo ‘</pre>’;

}

PHP , ,

Database – test int type

August 9th, 2010

這是一個非常有興趣測試,我嘗試設置一個field是id int(1), 另一個filed是name varchar(255)

CREATE TABLE `test` (

`id` INT( 1 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL

) ENGINE = innodb;

當我進行9次insert name的時候, 我就疑問, 第十個insert 將會發生error,
可是我繼續insert的時候, 他是沒有發生這個錯誤, 而且可以繼續增加到int 直到最大數目 – 2147483647

insert into `test` (`name`)  VALUES  (‘name 1’), (‘name 2’), (‘name 3’), (‘name 4’), (‘name 5’), (‘name 6’), (‘name 7’), (‘name 8’), (‘name 9’), (‘name 10’)

測試後其實int 是可以超出設定上限, 直到最大上限
覺得有趣的是設定上限後, 也能超出, 為什麼mysql不一開始就好似text, 不用預設一些上限
總是我不設int上限, 他也會幫我設定int(11)

MYSQL , ,

HTML disable input text history

August 7th, 2010

主要防止一些javascript執行(onchange, onclick)的時候,沒有回應
<input type=”text” autocomplete off />
這樣就能取消text box history

HTML , ,

Insert Data in mysql

July 30th, 2010

Assuming a nicely formatted ‘|’ delimited text file that we want to insert into the table above, take this piece of PHP code:

if (!($fp = fopen("datafile.txt","r"))) {               // open the file for reading
  print "\nUnable to open datafile.txt for writing";    // display error
  exit();                                       // end the running of the program
}

while (!feof ($fp)) {                   // loop through the file line by line
  $sline = fgets($fp, 4096);            // put the data into the variable $sline
  $sline = chop($sline);                        // remove the newline
  list($eno,$fname,$sname,$telno,$salary) = split("|",$code);
                        // split the line on "|", populating the ind. variables
  $db->query("insert into employee(employee_number,firstname,surname,
tel_no, salary
   values($eno,'$fname','$sname','$tel_no', $salary)");
}                                               // end while loop

This would work, but would be very slow. The index buffer would be flushed after every insert. Until recently, MyISAM tables (The MySQL default) did not allow data to be inserted at the same time as being read. The new format does, but only if there are no deleted records present (highly unlikely in a heavily used system). So the entire table is locked for the duration of each insert. Fine on a low volume site, but when you’re getting hundreds or thousands of queries per second, you’ll soon notice the backlog!

There’s a solution however – the best way to insert the data is to use MySQL’s “LOAD DATA INFILE“. This is much faster (20 times according to MySQL), and the only way some systems I’ve seen are still hanging in there!

The syntax is simple, and the code becomes a lot simpler too:

$db->query("LOAD DATA INFILE 'datafile.txt' INTO TABLE employee (employee_number,firstname,surname,tel_no,salary) FIELDS TERMINATED BY '|'");

LOAD DATA INFILE has defaults of:

FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'

if you don’t specify any of these clauses. And, just as with an ordinary insert, you need to specify a field list if the order of the fields is different, or, as in the example above, you’re not inserting data for every field. Always specifying a field list is good practice for all queries anyway – if someone adds a field to the table at a later stage, you don’t want to go back and have to fix all your previous INSERT and SELECT * statements.

If you can’t get this to work properly, have a look at the format of your text file – every problem I’ve seen with LOAD DATA has been because of a corrupted text file. Every field in every row must be delimited correctly!

You may not always be inserting from a text file – perhaps your application needs to do many unrelated inserts continually. There are ways to make sure the mass of users selecting are not badly affected… The first is to use INSERT LOW PRIORITY. This waits until there are no more reads waiting to happen, waiting for the gap, and not pushing in as it were. Of course, if your database is a rush hour special, there may never be a gap, and the client performing the INSERT LOW PRIORITY may start to grow cobwebs! An alternative here is INSERT DELAYED. The client is immediately freed, and the insert put into a queue (with all the other INSERT DELAYED‘s still waiting for the queue to end). This means that there can be no meaningful information passed back to the client, (such as the auto_increment value), as the INSERT has not been processed when the client is freed. Also, be aware that a catastrophe such as an unexpected power failure here will result in the queued INSERT‘s being lost. For neither of these methods do you have any idea when the data will be inserted, if at all, so I suggest you use with caution.

Database ,