Home >Backend Development >PHP Tutorial >Top Ten Tips to Improve Your PHP Application Strength_PHP Tutorial
Agni Website Building Academy Documents On the Smashing Magazine website, Glen Stansberry proposed ten advanced PHP tips that can instantly improve your PHP programming skills, including a "cheat sheet" for SQL injection attacks ”, simplify the else part of the judgment sentence, use regular expressions, ternary operators, Memcached database caching system, etc. only when necessary. The following is the detailed content.
PHP started as a humble programming language in 1995. It has developed rapidly over the years and is now one of the most popular web development languages. Many popular websites are developed using PHP, and the vast majority of programs and websites Projects are written in this popular language.
Because PHP is so popular, it is almost impossible for any website developer not to know PHP. This tutorial is for those who have just completed the initial stage of learning PHP and roll up their sleeves and are ready to work hard. Listed below The ten excellent tips that PHP developers must learn and use every time they program. These tips can speed up your proficiency in PHP, making the code run faster, more concisely, and with better performance.
SQL injection attack is a very nasty thing, it is a security vulnerability that allows hackers to sneak into your database by exploiting holes in the code. Although this article has nothing to do with MySQL, many PHP programs use the MySQL database, and if you want to write secure code, it is useful to know what situations to avoid.
Furruh Mavituna wrote a very interesting SQL injection attack cheat sheet, including a chapter about the security vulnerabilities of PHP and MySQL. If you can avoid the behaviors described in it, your program will not be vulnerable to attacks. .
Comparison operators are a very important part of PHP. Unfortunately, many programmers are not familiar with the differences between them. In fact, according to an article published on the IO Reader website, many PHP developers cannot distinguish between PHP operators. The differences between comparison operators, quote a few sentences from the article:
This is extremely important, but many PHP developers simply don’t know the difference between == and ===. Essentially, == determines whether two values are equal, and PHP will try to convert the data before comparing it. The type starts with, for example: 1== '1' (true); === determines the value and data type at the same time, for example: 1==='1' (false). Developers must understand the impact of these operators on commonly used functions as soon as possible. The practicality of formulas such as strpos(), because PHP treats zero as "false", without === we have no way of knowing whether a searched string is at the beginning of a longer string, or does not exist at all. In many cases In other applications, === is useful when returning a value of zero does not necessarily equal false.
You can go to the PHP.net website to see a list of various comparison operators.
I must remind everyone that the third and fourth points may reduce the readability of the code. These two points emphasize speed and performance. If you don’t want to sacrifice the readability of the code, you may want to skip them. .
Anything that can make the code simpler and smaller is usually a good practice. One of them is to extract the content of the else statement. Christian Montoya has a good example of using a shorter else statement to convert characters.
Common else narrative sentences:
The following is the quoted content:
{ $x = 5;<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font color="#ff0000">以下为引用的内容:</font><br /><pre class="brush:php;toolbar:false">$x = 10;<br />if( this condition )<br />{<br />$x = 5;<br />}} <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">if( this condition )</span>如果(这种情况)</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">{</span> (</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">$x = 5;</span> $ x = 5 ;</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">}</span> )</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">else</span>其他的</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">{</span> (</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">$x = 10;</span> $ x = 10 ;</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">}</span> )</span>else | {
以下为引用的内容: if ($gollum == 'halfling') { $height --; } |
The following is the quoted content: if ($gollum == 'halfling' ) {$height --;}
这是相同的:<br>
这个方法可以在程式中多次使用:<br><br>
5. 取 str_replace(),捨 ereg_replace() 及 preg_replace() 以效率来说,str_replace() 比正规表达式更适合用来取代字符串,据一些研究,str_replace() 的效率比 ereg_replace() 和 preg_replace() 高 61%。 <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">$x = 10;</span> $ x = 10 ;</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">if( this condition )</span>如果(这种情况)</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">{</span> (</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">$x = 5;</span> $ x = 5 ;</span> <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">}</span> )</span>
|