日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

Top 11 Best Practices for PHP Development

發(fā)布時(shí)間:2023/12/14 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Top 11 Best Practices for PHP Development 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

Right from its inception, PHP was widely used to develop web based applications. Since PHP is a scripting language, one must follow some rules while developing.

This article will discuss the best practices that are generally followed in the PHP world.

?1. Error reporting should be turned on

Error reporting is a very useful function in PHP and should be enabled while in the development phase. This helps us to identify the problems in our code. The most commonly used feature is 'E_ALL', which helps us to spot all the warnings and critical errors. It must be noted that before we put our code into production, we should turn off this feature as this would expose all the potential errors on the browser.

? ? ?? Delivering Application Data On-Demand?? Download Now ? ? ? ?

2. Use the DRY approach

DRY stands for Do not Repeat Yourself'. This concept is a very useful programming concept and should be used in any programming language, such as Java, C# or PHP. Using the DRY approach we ensure that there is no redundant code. A piece of code that violates DRY is referred to as WET solution. WET stands for write everything twic or we enjoy typing. Let us have a look into the following code:

Listing 1: DRY and WET approaches

$mysql??=?mysql_connect?(?'localhost',??'mysqladmin_uid',?'mysqladmin_pwd'?); mysql_select_db(?'DB_NAME'?)?or?die(?"Sorry?!!?No?database?selected!");

?The code above is based on the WET approach as the relevant parameters are hardcoded. Following the DRY approach, the code can be updated to:

$db_host?=?'?localhost?'; $db_user?=?'?mysqladmin_uid?'; $db_password?=?'?mysqladmin_pwd?'; $db_database?=?'?DB_NAME?';? $mysql?=?mysql_connect($db_host,?$db_user,?$db_password); mysql_select_db($db_database);

?3. Indentation and Use of whitespace

While writing code in any programming language, you must ensure that the code is properly indented and sufficient white space is provided wherever required. This increases the readability of the code and helps us to maintain the code in a more efficient manner.

?

4. Meaningful and consistent naming standards

As in any programming language, PHP experts also advise to follow meaningful naming standards. We have two major approaches while we ensure to implement this:

? ? ? ? Using Camel Case– In this approach, the first letter is in lower case and first letter of every word thereafter is in upper case.

Listing 2: Code snippet using camel case

public?class?MyClass?{public?void?methodName(String?argName)?{} }

?? ? ? ? Using underscores between two words– In this approach, we put an underscore character ('_') between every two words. Using this approach, the code can be modified as follows:

Listing 3: Code snippet using underscores

public?class?MyClass?{ public?void?method_name(String?arg_name)?{ } }

?5. Deep Nesting should be avoided

Multilevel nesting reduces the readability of the code regardless of programming language. Any programmer should avoid using deep nesting.

Listing 4: Code snippet having multi level nesting

public?class?MyClass?{ public?void?method_name(String?arg_name)?{ if?(is_writable($folder))?{if?($fp?=?fopen($file_location_path,'w'))?{if?($stuff?=?extractSomeConditionalStuff())?{if?(?fwrite?(?$fp,?$stuff)?)??{//?...}??else??{return?false;} }?else?{return?false; } }?else?{return?false; } }?else?{return?false; } } }

?The code above is a simple nested code. As we can see it is very difficult to figure which if block ends where. For a better readability, let us modify the code:

Listing 5: Code snippet avoiding multi level nesting

function?method_name?(String?arg_name)?{? //?...if?(!is_writable($folder))?{return?false;}if?(!$fp?=?fopen($file_location_path,'w'))?{return?false;}if?(!$stuff?=?extractSomeConditionalStuff())?{return?false;}if?(fwrite($fp,?$stuff))?{//?...}?else?{return?false;} }

?

6. Use adequate comments

As in any programming language, make sure that your source code has sufficient inline comments. This is a standard practice and should be followed. This helps in further analyzing the code base as it is a often the case that the person who is developing the code is not maintaining the same. Even if the same person is asked to make some changes in the code, inline comments will always be helpful to understand the motive of writing the code. In order to maintain high class comment standard in PHP I would recommend you familiarize yourself with a PHP documentation package such as phpDocumentor.

7. Do not put phpInfo() function in web root

phpInfo() is a very important function and should be used with utmost care. Using this function, anyone can get the details of the server environment. It is always advisable to keep the file containing phpInfo()function in a secured location. Once the development is done, it should be taken out of the code immediately.

8. Never trust the user

If your application involves any user input, write your code in such a way that it can handle all sorts of possible inputs. A good approach to protect your application from hackers is to always initialize your variables with some initial value that may not be relevant in the existing business flow.

9. Use Cache mechanism wherever required

Good programming approaches always suggest using the cache mechanism as the cache helps us to achieve better performance. In the PHP world, caching is achieved using:

? ? ? ? Memcached– an in memory key-value pair store used for small chunks of data.

? ? ? ? APC– Alternative PHP Cache an open opcode cache for PHP

? ? ? ? XCache– A fast reliable PHP opcode cache

? ? ? ? Zend Cache– A collection of APIs for realizing advanced caching capabilities.

? ? ? ? eAcclerator– Open source caching tool

10. Avoid copying extra variables

It is not a good programming practice to copy predefined variables into local variables having smaller names. This has an adverse effect on the performance of the application. Let us see the following code snippet:

Listing 6: Copying extra variables

$desc?=?strip_tags($_POST['PHP?description']); echo?$desc;

?The code snippet above is an example of copying a variable into a local variable unnecessarily. This is not at all a good practice. The same effect can be achieved by using the following code:

echo?strip_tags($_POST['PHP?description']);

?

11. Use frameworks

Frameworks are developed after a great deal of research and hence they prove to be less problematic. They make our lives easier as they provide proven solutions. In PHP there are lots of frameworks available. During development, you should make use of these. One of these frameworks that are widely used is MVC or Model View Controller.

?

Conclusion

? ? ? ? Best practices guide us to develop code in a more efficient manner.

? ? ? ? Following best practices ensures better performance of the application.

? ? ? ? As in other programming language, PHP also follows the best practices in the industry which ensures that the application developed is a good one.

?

About the Author

Kaushik Pal is a technical architect with 15 years of experience in enterprise application and product development. He has expertise in web technologies, architecture/design, java/j2ee, Open source and big data technologies. You can find more of his work at www.techalpine.com and you can email him here.


轉(zhuǎn)載于:https://my.oschina.net/ajian2014/blog/306730

總結(jié)

以上是生活随笔為你收集整理的Top 11 Best Practices for PHP Development的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国模小黎自慰gogo人体 | 日韩无砖| 三级黄色在线播放 | 四虎在线免费视频 | 亚洲av日韩av永久无码下载 | 精品一性一色一乱农村 | 国产日韩久久 | 樱花视频在线免费观看 | 99国产精品一区二区 | 91欧美一区二区三区 | 青青操在线观看 | 中文字字幕在线中文乱码电影 | 丁香av | 国产欧美精品在线观看 | 国产精品久久久爽爽爽麻豆色哟哟 | 成人网在线免费观看 | 日日夜夜精品视频 | 国产在线天堂 | 青青色在线观看 | 伊人网站在线观看 | 老头吃奶性行交 | 色撸撸av | 欧美毛片在线观看 | 欧美区一区二 | 天天操操夜夜操操 | 内射一区二区 | 少妇做爰免费视看片 | 毛片日本| 黄色在线免费视频 | 18性xxxxx性猛交 | 亚洲免费精品 | 亚洲自拍中文 | 一级片福利 | 午夜爱| 特高潮videossexhd | 免费看h网站 | 国产一区美女 | 久久综合综合久久 | ass精品国模裸体欣赏pics | 国产尻逼| 久久久久久久久久久久久久久久久久久 | 辟里啪啦国语版免费观看 | 中国黄色小视频 | 真人真事免费毛片 | 怡红院一区二区 | 国内外成人激情视频 | www.成人在线视频 | a点w片| 精品无码av一区二区三区不卡 | 天天夜夜久久 | 九九热国产视频 | 欧美日韩亚洲国产 | 中文字幕在线视频免费 | 国产视频导航 | 欧美精品做受xxx性少妇 | 亚洲av综合一区 | 黄色网址视频 | 成年人性生活视频 | 91亚洲成人| 久久免费毛片 | 午夜视频在线免费看 | 国产精品丝袜视频无码一区69 | 午夜在线视频播放 | 精品一区精品二区 | 日本一区二区三区久久久久 | 国产成人综合在线视频 | 丁香六月av| 国内av自拍| 日韩在线视频网 | 午夜小网站 | 欧美一区三区三区高中清蜜桃 | 精品无码人妻一区二区免费蜜桃 | 成人欧美一区二区三区黑人免费 | 久久久久久久女国产乱让韩 | 18男女无套免费视频 | 男人在线天堂 | 97热久久 | 男女啊啊啊视频 | 久久538| 国产黄色www | 日韩午夜网站 | 日韩啪啪网 | 欧美国产在线观看 | 黄色aaa毛片 | www.youjizz.com视频| 青青草视频播放器 | 国产黄色录像 | 色视频免费看 | 欧美高清久久 | 亚洲一级一区 | 天天爽夜夜爽夜夜爽精品 | 精品免费久久久 | 久久亚洲精选 | a免费在线| 欧美自拍偷拍一区二区 | 乳揉みま痴汉4在线播放 | 手机在线观看毛片 | 宿舍女女闺蜜调教羞辱 | 老男人av|