Home >Backend Development >PHP Tutorial >Top Ten Tips to Improve Your PHP Application Strength_PHP Tutorial

Top Ten Tips to Improve Your PHP Application Strength_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:56:23932browse

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.

1. Using SQL Injection Attack Cheat Sheet

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. .

2. Understand the differences between various comparison operators

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.

3. Simplify the else part of the judgment sentence

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:

{$x = 10;}
The following is the quoted content:
以下为引用的内容:
if( this condition )
{
$x = 5;
}
else
{
$x = 10;
}
if( this condition )

{

$x = 5;}else

If the default value of $x is 10, we start from 10 without specifying its value in the else part.

以下为引用的内容:
if ($gollum == 'halfling') {
$height --;
}
Although there doesn’t seem to be much difference, if there are many else statements in your program, the cumulative effect will be obvious. 4. Drop those parentheses Just like simplifying the else statement, if there is only one expression after the control structure, discarding the parentheses before and after the expression can save some characters. Evolt.org has a neat example of how to reduce the use of parentheses.
The following is the quoted content: if ($gollum == 'halfling' ) {$height --;}
这是相同的:<br>
以下为引用的内容:
if ($gollum == 'halfling') $height --;
这个方法可以在程式中多次使用:<br><br>
以下为引用的内容:
if ($gollum == 'halfling') $height --;
else $height ++;

if ($frodo != 'dead')
echo 'Gosh darnit, roll again Sauron';

foreach ($kill as $count)
echo 'Legolas strikes again, that makes' . $count . 'for me!';


5. 取 str_replace(),捨 ereg_replace() 及 preg_replace()

以效率来说,str_replace() 比正规表达式更适合用来取代字符串,据一些研究,str_replace() 的效率比 ereg_replace() 和 preg_replace() 高 61%。

  • 共2页:
  • 上一页
  • 1
  • 2
  • 下一页

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/364244.htmlTechArticle烈火建站学院文档 在 Smashing Magazine 的网站,Glen Stansberry 提出十个进阶 PHP 技巧,可以即时提昇你的 PHP 编程实力,其中包括 SQL 注入攻击的...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn