Warning: curl_exec() has been disabled for security reasons in /pub/host/sunboyu/sunboyu/www/wp-includes/http.php on line 1022
php 一个程序猿 孙小一,孙小二,PHP,MYSQL,LINUX,APACHE,原创技术,扯淡

腾讯公司的PHP面试题,看你能拿多少分

作者 : admin 于 2008年08月08日, 15:14:06
2008
08-8

40 Tips for optimizing your php code

作者 : admin 于 2008年08月05日, 10:22:25
2008
08-5
  1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
  2. echo is faster than print.
  3. Use echo’s multiple parameters instead of string concatenation.
  4. Set the maxvalue for your for-loops before and not in the loop.
  5. Unset your variables to free memory, especially large arrays.
  6. Avoid magic like __get, __set, __autoload
  7. require_once() is expensive
  8. Use full paths in includes and requires, less time spent on resolving the OS paths.
  9. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
  10. See if you can use strncasecmp, strpbrk and stripos instead of regex
  11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
  12. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
  13. It’s better to use switch statements than multi if, else if, statements.
  14. Error suppression with @ is very slow.
  15. Turn on apache’s mod_deflate
  16. Close your database connections when you’re done with them
  17. $row[’id’] is 7 times faster than $row[id]
  18. Error messages are expensive
  19. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
  20. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.
  21. Incrementing a global variable is 2 times slow than a local var.
  22. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
  23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
  24. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
  25. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance.
  26. Methods in derived classes run faster than ones defined in the base class.
  27. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.
  28. 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.
  29. When echoing strings it’s faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments.
  30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts.
  31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times.
  32. Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request
  33. When working with strings and you need to check that the string is either of a certain length you’d understandably would want to use the strlen() function. This function is pretty quick since it’s operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick.Ex.if (strlen($foo) < 5) { echo “Foo is too short”; }

    vs.

    if (!isset($foo{5})) { echo “Foo is too short”; }

    Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it’s execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string’s length.

  34. 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.
  35. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory.
  36. Do not implement every data structure as a class, arrays are useful, too
  37. Don’t split methods too much, think, which code you will really re-use
  38. You can always split the code of a method later, when needed
  39. Make use of the countless predefined functions
  40. If you have very time consuming functions in your code, consider writing them as C extensions
  41. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview
  42. mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%
  43. Excellent Article about optimizing php by John Lim

在windows在安装FreeTDS,让mssql支持UTF-8

作者 : admin 于 2008年07月22日, 15:27:47
2008
07-22

本人有个项目使用php+mssql,而mssql只支持gb2312和utf-16编码,而php又要求使用utf-8编码,想到了freetds.而freetds是个在unix下开发的工具,我下载freetds最新版后,发现里边有windows下的编译脚本,在DEV-C++里进行编译后,不支持,后又从鬼子论坛里找到达人编译好的组件,安装配置后,正常,大喜。

; Specify client character set.
; If empty or not set the client charset from freetds.comf is used
; This is only used when compiled with FreeTDS
mssql.charset = “UTF-8″

在php.ini里设置这里,即可让FreeTDS生效。This is only used when compiled with FreeTDS。mssql默认是不支持设置字符集的,只有安装freetds的时候才能生效。

其实,PHP官方早就在系统里留出了freetds的接口,windows下的PHP.INI文件就为freetds留出接口参数。

在linux下,编译php的时候,有这样的日志

configure:68696: checking whether to enable pcntl support
configure:69264: checking whether to enable PDO support
configure:69689: checking for PDO_DBLIB support via FreeTDS
configure:70399: checking for Firebird support for PDO

在linux下,同样为php留出了接口

打造全能优化的Linux+Apache+PHP+Mysql服务器(1)

作者 : admin 于 2008年07月13日, 21:05:51
2008
07-13

fastcgi   http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
apache2   http://apache.mirror.phpchina.com/httpd/httpd-2.2.9.tar.gz
mysql5    http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.51b.tar.gz/from/http://mysql.mirrors.arminco.com/
php5      http://cn2.php.net/distributions/php-5.2.6.tar.gz
libxml2
gd-jpeg   ftp://192.48.96.9/graphics/jpeg/jpegsrc.v6b.tar.gz
freetype  http://voxel.dl.sourceforge.net/sourceforge/freetype/freetype-2.3.7.tar.gz
libpng    http://voxel.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.29.tar.gz
zend      http://www.zend.com/download/55

memcached  http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
memcache   http://pecl.php.net/get/memcache-3.0.1.tgz
libevent   http://www.monkey.org/~provos/libevent-1.2a.tar.gz

目前想到的功能大概有这些,想到再加。

目前正在研究每一个组件的性能和参数。

学编程,重在基本素质

作者 : admin 于 2008年07月10日, 09:30:49
2008
07-10

最近总有朋友问我,国内有什么比较简单的PHP书籍,怎么学习PHP,有啥捷径,还有的说,二哥,给个小程序学学……

我一般都会说,没捷径,没好书,想学程序,去下一些外国的开源,比我的好得多。

其实,我也是从问那些问题过来的,不过,我还是想把我的经验分享给大家,希望大家别绕弯子,能走捷径。

学PHP,建议还是有点编程基础,不管是C,VB,VFP。懂点语言基础,就说明能理解一些基本概念。一些函数,变量,逻辑分支,这些是最基础的。有这些基础,再了解B/S结构工作原理,就可以写WEB程序了。

书,我一般当工具书,讲案例的不多,杜江老师那本书凑合,对我来说已经足够,能知道PHP都能干什么,其他的,你可以买本圣经当手册,也可以下个电子版。

程序,建议到站长网或者开源社区下一些小的,学就要学好的,看就要看规范的,看我的只能把你们带坏。先学着改人家的程序,可以改改DEDEcms,可以改一下ECSHOP,可以改改论坛,看看discuz,做个模板等。

看差不多了,可以自己写简单的程序,一般都是留言本(我最近才写过),写个简单的文章发布,足以。

这些都差不多了,可以看一些别人的类库,框架,看看人家是怎么封装一些逻辑过程。

这些你都学差不多了,其实找工作就不难了。别嫌工资少,找个项目,最好能有个好的大哥,拼一段,能力会有很大提高。

作项目,不是单纯做程序,要把一些程序之外的东西揉到程序里,这时候,才是一个有实用价值的程序员。

至于如何提高,我没有啥好的方法。我也不认为我提高多快。我的方法只是:不断敲代码,天天敲代码。

要有高人有啥好的方法,有啥好的课题,可以一起研究。

程序调式N多招

作者 : admin 于 2008年07月08日, 20:00:13
2008
07-8

最近在群里扯淡,好多小鸟提的问题其实很简单,程序本身的报错机制就能告诉你错误。现在列出所有能给你提示的方法.

  1. 打开PHP的报错。在php.ini(win下)中,寻找  error_reporting   ,设置 error_reporting  =  E_ALL & ~E_NOTICE,好像默认就是这个。
  2. 如果还是找不到,那么在你程序的头上加上:error_reporting(2047),这样错误也都会出来。

这两条是程序有硬伤的时候,直接报出来。如果逻辑上的错误,咋调呢?我的方法比较奔,但的确有效。

  1. 输入echo “aaa”;break; 这样,可以判断出程序是哪里出问题而中断了。
  2. print_r($var);这样去跟踪你认为出错的数据,用肉眼监视+大脑计算,去对比程序的计算,只要涉及变量值可能改变的地方,都得去对比。

这是程序差错的方法。mysql就更简单了,把你认为有错误的sql语句echo出来,放到phpmyadmin里去执行,看那个报错就行了。

最后一点:下载个星际译王,当然是指英文不太利索的。

Web Service来做简单认证服务器

作者 : admin 于 2008年07月08日, 11:21:43
2008
07-8

Web Service简介

Web Service主要是为了使原来各孤立的站点之间的信息能够相互通信、共享而提出的一种接口。 Web Service所使用的是Internet上统一、开放的标准,如HTTP、XML、SOAP(简单对象访问协议)、WSDL等,所以Web Service可以在任何支持这些标准的环境(Windows,Linux)中使用。注:SOAP协议(Simple Object Access Protocal,简单对象访问协议),它是一个用于分散和分布式环境下网络信息交换的基于XML的通讯协议。在此协议下,软件组件或应用程序能够通过标准的HTTP协议进行通讯。它的设计目标就是简单性和扩展性,这有助于大量异构程序和平台之间的互操作性,从而使存在的应用程序能够被广泛的用户访问。

最近写一个认证服务器,需要进行不同程序,不同数据库之间的数据交换,认证服务器提供认证功能,而客户端可能是多种语言开发的。如果实用传统的方式,比如C,或者java开发一个稳定的服务端,人力精力都是问题,只能寻求一种简单的方式进行过渡。

这里我选择了Web Service这种方式,但这种方式也存在一定问题:速度。网上普遍反映速度是问题,soap的方式本身负载是问题,实用xmlrpc,http方式,瓶颈在于webserver的负载能力。但是项目发展初期,此方式完全能满足一段时间,而这段时间我们也能平滑过渡,留出时间进行更深层次的研究。

PHPer市场供求关系调查

作者 : admin 于 2008年07月04日, 22:56:56
2008
07-4

所有招聘网站均直接搜索关键词 PHP

chinahr.com     共有2111条结果   全国

zhaopin.com   共 1042 个职位   北京

51job.com    约有 4412 项符合条件的查询结果  全国

约有21,200,000项符合php 培训的查询结果      GOOGLE搜索 PHP 培训 关键词

趋势是显而易见的,PHP在WEB领域独领风骚。人才呢,您自己思考。

今天申请了一个google code的帐号

作者 : admin 于 2008年06月28日, 16:30:41
2008
06-28

http://code.google.com/p/phpdeveloppackage/

免费,挺好用,管理自己的一些代码,挺好的。

以后拼命的结果就都在这里了。

PHP是草根语言,在中国是一种曲解

作者 : admin 于 2008年06月23日, 20:10:21
2008
06-23

突然看到一本图书的作者介绍,才发现,原来PHP在外国也是很风光的。

作者介绍:

Laura Thomson是澳大利亚墨尔本RMIT大学计算机科学信息技术学院的讲师。她也是Tangled Web Design公司的合伙人。Laura曾经在Telstra和波士顿顾问集团工作过。她获得了应用科学(计算机科学)的学士学位和工程学(计算机系统工 程)学士学位,目前她正在攻读适应性Web站点的博士学位。

 Page 3 of 4 « 1  2  3  4 »