合肥网站建设工作室

关注合肥工作室微信公众号,提供最新网站建设资讯

扫一扫微信二维码

优化php代码的小实例(三)

2010-06-07 13:09:22网站设计

0.使用checkdnsrr()通过域名存在性来确认部分email地址的有效性,这个内置函数能保证每一个的域名对应一个IP地址;toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
1.如果你在使用php5和mysql4.1以上的版本,考虑使用mysql_*的改良函数mysqli_*;toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
2.试着喜欢使用三元运算符(?:);toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
3.在你想在彻底重做你的项目前,看看PEAR有没有你需要的。PEAR是个巨大的资源库,很多php开发者都知道;toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
4.使用highlight_file()能自动打印一份很好格式化的页面源代码的副本;toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
5.使用error_reporting(0)函数来预防潜在的敏感信息显示给用户。理想的错误报告应该被完全禁用在php.ini文件里。可是如果你在用一个共享的虚拟主机,php.ini你不能修改,那么你最好添加error_reporting(0)函数,放在每个脚本文件的第一行(或用require_once()来加载)这能有效的保护敏感的SQL查询和路径在出错时不被显示;toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
6.使用 gzcompress() 和gzuncompress()对容量大的字符串进行压缩(解压)在存进(取出)数据库时。这种内置的函数使用gzip算法能压缩到90%;toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
7.通过参数变量地址得引用来使一个函数有多个返回值。你可以在变量前加个“&”来表示按地址传递而非按值传递;toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
8.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.toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
9.使用strlen()因为要调用一些其他操作例如lowercase和hash表查询所以速度不是太好,我们可以用isset()来实现相似的功能,isset()速度优于strlen();toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
10.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. toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室
 toH合肥网站工作室|合肥网站建设优化推广 - 合肥LZ建站工作室

本文关键词
php代码实例,php优化实例