13 PHP tips and tricks that can make Developers life easier

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
php has a pretty bad reputation being a poor cousin of scripting languages . At conferences, no presenter wants to be speak about it andthe truth is that the many atrocities that make PHP developers emboldened by its flexibility and simplicity have a good share of the blame. However, it is one of the most widely used language for web development, and the people who love and embrace PHP are truly making great contribution by developingexcellent frameworks and content management systems which gets things done easily and smartly and brings it at par with another modern languages.

In the blog post we will give you some tips, tricks and best practices (13 in particular) of PHP for you to code better, your professional life can be more enjoyable and can go to developer conferences with high head and without hiding the elephant :P.

Develop with error reporting enabled

One of the great nemesis of web developers in general and PHP errors in particular is the “white screen of death” . Error 500 gives no information and that can be extremely frustrating. To avoid this , without having to modify the php.ini file during the development phase you need only include the following two lines at the beginning of the code:

error_reporting ( E_ALL ) ; ini_set ( 'display_errors' , 1 ) ;

This lets you see both fatal errors that produce the dreaded white screenas warnings and notices that may be bugs to fix. Then, of course, do not forget to remove the board production.

Prevents SQL injection

Possibly one of the causes of bad reputation of PHP are security holes (Cross-Site Scripting, Cross-Site Request Forgeries) that can be cast as you’re not a little finicky. Of these the best known (and the easiest to prevent) is the SQL Injection : “ inserting SQL code invader in order to disrupt the normal operation of the program and ensure that the portion of invasive code to run in the database” with direct consequences.

How do we avoid it ?

There are several ways but the simplest is always escaping any variable (and not only the user inputs) that we will use the database. Like this:

$query_result = mysql_query ( "SELECT * FROM WHERE Ex_table ex_field = \" " . mysql_real_escape_string( $ ex_field ) . " \ " " ) ;

HowDoes using an outdated driver instead of MySQL MySQLi or PDO? And you are doing consultations here? Tranquility, we do not anticipate events of other tricks and tips that will come afterward.

Retire the MySQL driver

We are not in 2005 nor we still use PHP4 (or at least I hope) so we can now undo the MySQL driver to connect to, where you look, MySQL. We can bet on the driver MySQL (especially in its object oriented way ) or better yet, fall into the burly arms of PDO.

PDO , which stands for PHP Data Object is a PHP extension that provides an abstraction layer for connecting with different managers databases (because PHP can also work with databases other than MySQL although sometimes not seems). Come on , saving the distances can be compared to Java Hibernate and its use is as simple as we show below:

try { $conn = new PDO ( "mysql: host = localhost; dbname = database ' , $ user , $ pass ) ; $conn -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION ) ; } Catch ( PDOException $e ) { Echo "ERROR:" . $e -> getMessage ( ) ; }

Just remember to have installed and activated the extent and dust off your PDO notes.

13 PHP tips and tricks that can make Developers life easier
Embraces cURL

Okay, when you need to retrieve a file from another server, file_get_contents() is powerful, easy to use and gets you out of trouble but you have no control over what happens and flexibility is also poor. Come on, it’s like having death on your computer. So better pull the popular extension cURL (be sure to have it installed and activated before use), more powerful, flexible and with dozens of options. An example of use of cURL can be as simple as the following:

$c = curl_init ( ) ; curl_setopt ( $c , CURLOPT_URL , $URL ) ; curl_setopt ( $c , CURLOPT_TIMEOUT , 15 ) ; curl_setopt ( $c , CURLOPT_RETURNTRANSFER , true ) ; $content = curl_exec ( $c ) ; $status = curl_getinfo ( $c , CURLINFO_HTTP_CODE ) ; curl_close ( $c ) ;

You open a connection to a URL for a certain time and recover the contents therefore in addition to the status of the operation and not forgetting close it . Then the options you have to complicate everything are huge, clear (headers, POST parameters, authentication, different outputs, FTP connections, SSH connections, encryption).

cURL If you fall short you can always pull bookstores like GuzzleHTTP.

Use the _once() function with caution

When calling other files, libraries or classes we can choose the function include() or function require(). The first given a warning not found the file and continues execution. The second gives a fatal error and stops execution. Well, so far unclear, 101 PHP manual.

But we also have the option to use features include_eleven() and require_eleven(), which have the same functionality but prevent the files, classes or loaded libraries can be loaded again causing duplication and undesired states in the code. This is great to get priority but at the cost of decreased performance quite noticeable. Therefore almost better take care yourself, whenever possible, to avoid these duplication byreviewing the code and all calls in it.

Learn to handle ternary operators

Ternary operators are a good alternative to simple IF constructions: on the same line have the conditional and results of TRUE and FALSE . Here ‘s an example:

$name = ( !empty ( $ _ GET [ 'name' ] ) ? $ _ GET [ 'name' ] : 'John' ) ;

The variable will have the value of the GET parameter and have not, will the literal John. If you are dyslexic or just something distracted same costs you a bit of this ternary operators but to change your code will be lighter. They can be nested without any problems.

Use a switch instead of stringing Ifs like crazy

We continue our struggle against use If constructions for everything. The switch is slightly faster than If but leaving aside the performance issue, it is horrible to see that obscene amount of if-else if-else chains that make some codes unreadable.Use Switch, that’s what PHP5 as modern language and brings this construction,

a trick for Switch: put before the cases that you think are going to be used, performance will be even better. Example:

switch ($color ) { case 'blue' : echo "The color is blue" ; break ; case 'red' : echo "The color is red" ; break ; case 'turquoise' : echo "The color is turquoise" ; break ; case 'black' : echo "Color is black" ; break ; } Use single quotes rather than double

This may seem a truism but use single quotes (”) instead of double quotes (“”) is twice as fast . So use it whenever you can strip single quotes. The performance of your server will thank you.

Clean URLs quickly with .htaccess

URLs that generates PHP can be a disaster. Solutions for cleaner and more friendly URLs for both the human eye and for SEO there are many but few as fast and simple as “hack” the file .htaccess.

.htaccess Is a hidden file with Apache directives that serves a lot of things (here a fairly extensive and exhaustive tutorial), including performing redirection and clean URLs do not cease to be redirected to after all .

RewriteEngineOn RewriteRule ^ ( [ a - zA - Z0 - 9 ] + ) $ index . Php

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

tags: PHP,use
本文标题:13 PHP tips and tricks that can make Developers life easier

技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(179)