过去的几周对我来说是一段相当复杂的经历。我们公司进行了大裁员,我是其中之一,但却体验到了其中的乐趣。我从来没有被开除过,所以很难不去想得太多。我开始浏览招聘板块,一个全职PHP程序员的职位很吸引人,所以我寄去了简历并获得了面试机会。在面试之间,我和其主要的程序员们在咨询电话中聊了聊,最后他们给我出了一套测试题,其中有一道很耐人寻味。
找出以下代码的错误之处:
<?<BR>function baz($y $z) {<BR> $x = new Array();<BR> $x[sales] = 60;<BR> $x[profit] = 20:<br><br> foreach($x as $key = $value) {<BR> echo $key+" "+$value+"<BR>";<br> }<br>}
你能找到几个呢?
如果你发现函数参数列表中少了逗号、“new Array()”是不正确的、行末用了冒号而不是分号、foreach中没有用“=>”及用“+”来连接字符串,那恭喜你,你找到了所有的错误,你已经掌握了PHP编程的基础。
现在我来说说我是怎么回答这道题的。我当然也找出了以上这些问题,但我更进一步。比如,你有没有发现在数组索引里没有用引号将字符串括起来?虽然这不会造成严重错误,但这是一个编码错误。另外,你注意到在echo一行它使用了双引号而不是单引号吗?使用了PHP开始标志的缩写形式?并且没有用“
”而是用了“
”?
在找出了实际错误后,我又在上面找到的问题后面加了注释。这足够让这份答卷从“正确”转变为“发人深省”了,这也给我的申请加了不少分,所以他们决定聘用我。(但最后我拒绝了,因为我喜欢紧凑的生活节奏,并将自己的PHP技能奉献给我的客户,而不是一家涉猎电信市场的公司。我需要一个舞台来大展身手。)
那么接下来就来看看我写的10条PHP编程习惯吧:
1、使用单引号括起来的字符串
当使用双引号来括字符串时,PHP解释器会对其进行变量替换、转义等操作,如“\n”。如果你只想输出一个基本的字符串,就用单引号吧,这样会节省一些资源。当然,如果你需要进行变量替换的,那就必须用双引号了,但其他情况下还是用单引号吧。
2、字符串的输出
你认为以下哪一条语句的运行速度最快?
print "Hi my name is $a. I am $b";
echo "Hi my name is $a. I am $b";
echo "Hi my name is ".$a.". I am ".$b;
echo "Hi my name is ",$a,". I am ",$b;
echo 'Hi my name is ',$a,'. I am ',$b;
也许这看起来很奇怪,但事实上最后一条的运行速度是最快的。print比echo要慢,在字符串中进行变量替换时会慢,而连接字符串要比用逗号连接来得慢,最后一句则是第一个习惯的体现。所以,不在字符串中进行变量替换不仅会加快程序运行速度,也会让你的代码在任何语法高亮显示的编辑器中显得更为易懂(变量会被高亮显示出来)。很少人知道echo的参数可以用逗号连接,且速度会比字符串连接要来得快。最后再用上第一个习惯,那这条语句就非常好了。
3、在数组索引中使用单引号
正如你在上面的测试题中所看到的,我指出了$x[sales]从严格意义上来说是错误的,索引应该被括起来,即$x['sales']。这是因为PHP会将没有括起来的索引辨认为“裸”字符串,并把它解释为一个常量。当找不到该常量的定义时,才将其解释为一个字符串,所以这条语句才是可运行的。把索引括起来可以省去这部分工作,如果将来正好要用这一字符串定义常量时也就不会有错误了。我甚至听说这样做要快七倍左右的时间,虽然我没有亲自测试过。更多关于这一话题的讨论,请看PHP手册“数组”一章中的的“数组的能与不能”一节。
4、不要使用开始标志的缩写形式
你正在使用这样的符号吗?“”是非常糟糕的符号,它会引起与XML解释器的冲突。而且一旦你发布了这些代码,那么使用者就必须修改php.ini文件来打开对此符号的支持。所以实在没有理由去使用这种形式。用“
5、尽量不要使用正则表达式
在进行常规的字符串操作时,尽可能不要去使用正则表达式(preg和ereg系列函数)。str_replace函数要比preg_replace快得多,甚至strtr函数也要比str_replace来得快。省去这些不必要的麻烦吧,你的老板会感谢你的。
6、不要在循环声明中使用函数
这个问题不单单出现在PHP中,你可以在其他语言的代码中经常看到:
Difference: for($i=0;$i Good: $count=count($array);for($i=0;$i<$count;$i++){...} This should be easy to explain, but many people just want to write one less line of code and waste system resources. If the count function is used in the loop declaration, it will be called once for each loop. If you have a lot of loops, it will waste a lot of time. 7. Never use register_globals and magic quotes These are two very old functions that may have been a good approach at the time (ten years ago), but it seems not to be the case now. Older versions of PHP will turn on these two functions by default when installed, which can cause security holes, programming errors and other problems. For example, variables will only be created when the user enters data. Both features are now deprecated and should be avoided by every programmer. If your past programs used these two features, eliminate them as soon as possible. 8. Be sure to initialize variables ("Initialization" here refers to "declaration" - translator's note) When an uninitialized variable is required, the PHP interpreter will automatically create a variable, but it is not a good idea to rely on this feature for programming. This can make the program crude or confusing because you need to figure out where the variable was created. Additionally, incrementing an uninitialized variable is slower than an initialized variable. So it would be a good idea to initialize variables. 9. Comment the code This question has been raised many times, but no amount of times is enough. I know some places won't hire programmers who don't comment their code. After a previous job interview, I went through the code I wrote with the vice president and interviewer. When they were impressed by the code comments I made, they also learned about this habit of mine. One day later, I got the job. I know some people who call themselves PHP masters claim that their code is well written and does not require any comments. In my opinion, these people are trash. It is worthwhile to learn the specifications and techniques for writing comments, and become familiar with comment assistance software such as phpDocumentor or Doxygen. 10. Follow a programming specification About this, you need to ask your potential boss during the interview, ask them what programming conventions they are using. PEAR? Zend? Internal regulations? Mention the programming convention you are using, whether it is one of your own creation or one that is currently popular. For a loose language like PHP, if there is no good programming specification, then the code will look like a bunch of garbage. Stinky, disgusting garbage. Some basic specifications include space specifications, bracket matching, naming style, etc. This is a must for anyone who pursues high-quality code. Someone said: "I hate your 4-space indentation." I want to say, what? Use 4 spaces for indentation? This takes up 3 characters more space than using tabs. More importantly, you can customize the tab indentation value as long as you use an editor more advanced than Notepad. So every programmer can look at the code in the way they're most comfortable with. Set to 4 when possible, or 0 if you're a masochist. I don't care anyway, but you just can't use spaces for indentation! In general, I hope the above programming habits can be helpful to you. If you want to make a good impression during an interview, all it takes is a few small details.