您的位置:首页 > 编程语言 > PHP开发

Ten PHP Best Practices Tips that can Help You to Get a Job

2015-01-20 14:54 459 查看
Find out what is not right or what are the mistakes in the below mentioned code;你认为下面代码有哪些错误?

<?
function baz($y $z) {
$x = new Array();
$x[sales]  = 60;
$x[profit] = 20:
foreach($x as $key = $value) {
echo $key+" "+$value+"<BR>";
}
}
?>

如果你发现函数参数列表中少了逗号、“new Array()”是不正确的、行末用了冒号而不是分号、foreach中没有用“=>”及用“+”来连接字符串,那恭喜你,你找到了所有的错误,你已经掌握了PHP编程的基础。但更进一步,你有没有发现在数组索引里没有用引号将字符串括起来?虽然这不会造成严重错误,但这是一个编码错误。另外,你注意到在echo一行它使用了双引号而不是单引号吗?使用了PHP开始标志的缩写形式?并且没有用 “”而是用了“”?

The website development has become the latest trend and requirement of today’s business. This industry has a high scope. No PHP Developer can stay unemployed if he takes a right decision of right learning ways. Here I am sharing with you some tips that will
help you to get best job in your industry.

网站的发展已成为当今企业的最新趋势和要求。该行业具有较高的前景。如果一个PHP开发者做了正确的选择——采用正确的学习方式,那他一定会被重用。我在这里跟大家分享一些技巧,这些技巧将帮助您获得最好的工作。

1、使用单引号括起来的字符串当使用双引号来括字符串时,PHP解释器会对其进行变量替换、转义等操作,如“\n”。如果你只想输出一个基本的字符串,就用单引号吧,这样会节省一些资源。当然,如果你需要进行变量替换的,那就必须用双引号了,但其他情况下还是用单引号吧。

2、字符串的输出Do a test to know your skill proficiency level. For example,

The next test is to find out; which line of code do you think runs faster?

你认为下面哪一行代码执行得更快?

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;

也许这看起来很奇怪,但事实上最后一条的运行速度是最快的。print比echo要慢,在字符串中进行变量替换时会慢,而连接字符串要比用逗号连接来得慢,最后一句则是第一个习惯的体现。所以,不在字符串中进行变量替换不仅会加快程序运行速度,也会让你的代码在任何语法高亮显示的编辑器中显得更为易懂(变量会被高亮显示出来)。很少人知道echo的参数可以用逗号连接,且速度会比字符串连接要来得快。最后再用上第一个习惯,那这条语句就非常好了。In PHP coding, even a single mismanaged comma
or semi-colon matters and it can ruin all the coding!

Although it might look like wastage of time or weird but it would test your PHP proficiency better than any other typical looking test.

在PHP编程中,甚至一个单一的不合适的逗号或分号问题都可以毁掉所有的编码!

尽管这个测试可能看起来是一种时间的浪费、很怪异,但它会相对于其他任何典型测试更好地测试你的PHP能力。

3. Next, you would need to test with array indexes. Generate a code using single-quotes around the array indexes. This will help you to correct the technically incorrect codes.3. 在数组索引中使用单引号 正如你在上面的测试题中所看到的,我指出了$x[sales]从严格意义上来说是错误的,索引应该被括起来,即$x['sales']。这是因为PHP
会将没有括起来的索引辨认为“裸”字符串,并把它解释为一个常量。当找不到该常量的定义时,才将其解释为一个字符串,所以这条语句才是可运行的。把索引括起来可以省去这部分工作,如果将来正好要用这一字符串定义常量时也就不会有错误了。我甚至听说这样做要快七倍左右的时间,虽然我没有亲自测试过。更多关于这一话题的讨论,请看PHP手册“数组”一章中的的“数组的能与不能”一节。

4. For better results, it is recommended for you to not to use the short open tags like <?. Although it seems to be the easiest code but it is not good at all. It conflicts with the xml phrases and this might grow the additional errors on your page. So, the
full open tag <?php should be used instead of <?.4. 不要使用开始标志的缩写形式 你正在使用“<?”吗?是非常糟糕的符号,它会引起与XML解释器的冲突。而且一旦你发布了这些代码,那么使用者就必须修改php.ini文件来打开对此符号的支持。所以实在没有理由去使用这种形式。用“<?php“吧。

5. Do not use common or regular expression if not necessary. For basic string operations, preg and ereg function groups should be ignored. In this case, you can try str_replace because it works much faster than the ordinary code of preg_replace. Moreover, the
strtr function is even faster than the function of str_replace!5. 尽量不要使用正则表达式 在进行常规的字符串操作时,尽可能不要去使用正则表达式(preg和ereg系列函数)。str_replace函数要比preg_replace快得多,甚至strtr函数也要比str_replace来得快。省去这些不必要的麻烦吧,你的老板会感谢你的。

6. No additional functions should be used inside a loop declaration. For example,6. 不要在循环声明中使用函数 这个问题不单单出现在PHP中,你可以在其他语言的代码中经常看到:

Well defined code;好:

$count = count($array);
for($i = 0; $i < $count; $i++) {
//stuff
}
Bad set of code;差:
<?
for ($i = 0; $i < count($array); $i++) {
//stuff
}


7. The register_globals or any other magic quotes might look like the best one but it is advised to not to relay on them. These are the old PHP features which were thought to be good but they did not worked well according to the expectations. Sometimes,
they can cause the security hole and some programming errors. Now, both features have criticized and it is recommended to stop using them.7. 永远不要使用register_globals和magic quotes 这是两个很古老的功能,在当时(十年前)也许是一个好方法,但现在看来并非如此。老版本的PHP在安装时会默认打开这两个功能,这会引起安全漏洞、编程错误及其他的问题,如只有用户输入了数据时才会创建变量等。如今这两个功能都被舍弃了,所以每个程序员都应该避免使用。如果你过去的程序有使用这两项功能,那就尽快将其剔除吧。

8. Sometimes, if your code is not initialized then the PHP automatically creates variable and it is not good for future performance and it might lead to some errors in a specific functions. So, it is recommended to initialize every single variables of your
code for best result and reliability.8. 一定要对变量进行初始化(这里的“初始化”指的是“声明”——译者注) 当需要没有初始化的变量,PHP解释器会自动创建一个变量,但依靠这个特性来编程并不是一个好主意。这会造成程序的粗糙,或者使代码变得另人迷惑,因为你需要探寻这个变量是从哪里开始被创建的。另外,对一个没有初始化的变量进行递增操作要比初始化过的来得慢。所以对变量进行初始化会是个不错的主意。

9. The documentation of codes should be done for the reference. A developer writes so many codes and so the documentation might look a useless or heavy task but it is very important. You can take help of phpDocumentor or Doxygen which will help you in the documentation
of your codes. They will not take much time in documenting.9. 应当撰写代码的文档以供参考。开发人员写了太多的代码,写文档可能看起来是无用的或沉重的任务,但它是非常重要的。你可以通过phpDocumentor或Doxygen的帮助,

10. The most importantly, your coding method should follow the basic and if possible then all coding standards. If you write the codes according to the coding standards then the possibilities of errors reduces up to 80%! You can try using any text-editor or
notepad for the coding purpose. Some PHP editors are also available that will help you in identifying the difference between wrong and right code.10. 最重要的是,你的编码方法应至少遵循基本的编码标准,如果可能的话最好是遵循所有的编码标准。如果根据编码标准编写代码,则错误的可能性减小80%!为了编程,您可以尝试使用任何文本编辑器或记事本软件。你也可以找到一些PHP编辑器,这将帮助您识别错误和正确的代码之间的区别。

英文原文 http://3w.dokisoft.com/ten-php-best-practices-tips-that-can-help-you-to-get-a-job/ 中文参考 http://topic.yingjiesheng.com/ITjob/PHP/052211401952013.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 编程风格
相关文章推荐