Top ten tips to improve your PHP application Strength _php Tutorial

Source: Internet
Author: User
Tags php and mysql sql injection attack
Fire Station Academy documentationAt Smashing Magazine's website, Glen Stansberry presents 10 advanced PHP tips that can instantly lift your PHP programming prowess, including the "cheat sheet" for SQL injection attacks, simplify the else part of the sentence, and use the regular table in the last resort Dashi, ternary operators, Memcached Database cache system, and so on, the following is the detailed content.

PHP started in 1995 from a humble programming language, and has grown rapidly over the years, and is now one of the most popular web development languages, with many popular websites developed using PHP, and most programs and Web projects are written in this popular language.

Since PHP is so popular, almost all Web developers don't have to know PHP, this tutorial is for those who have just finished PHP learning the beginning, roll up the sleeves ready to do a good job, the following list of 10 excellent skills, is the PHP developers must learn and use them every time programming, These tips can speed up your proficiency in PHP, making your code run faster, more concise, and more optimized for performance.

1. Using SQL injection to attack the cheat sheet

A SQL injection attack is a very nasty thing, it is a security vulnerability that allows hackers to exploit vulnerabilities in code to sneak into your database. Although this article is not related to MySQL, many PHP programs use MySQL database, if you want to write secure code, it is useful to know what to avoid.

Furruh Mavituna wrote a very interesting SQL injection attack cheat sheet, one of the chapters on PHP and MySQL security vulnerabilities, if you can avoid the lines described in it, your program will not be vulnerable to attack.

2. Recognize the difference between the various comparison operators

The comparison operator is a very important part of PHP, but a lot of programmers are not familiar with the difference between them, in fact, according to an article published on the IO Reader website, many PHP developers can not distinguish between various PHP comparison operators differences, citing the text of a few words:

This is very important, but many PHP developers do not know that = = and = = = What is the difference, in essence, = = To determine whether the two values are equal, PHP will try to convert the data type before comparison, for example: 1== ' 1 ' (true); = = = value and data type, for example: 1 = = = ' 1 ' (false), developers must quickly understand the usefulness of these operators for commonly used functions such as Strpos (), since PHP sees zero as "false", and 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 other applications, = = = = = = = = = = = = = = = is useful when the return value of 0 is not equal to false.

You can see a list of the various comparison operators on the Php.net website.

3. Simplifying the else part of a judgment sentence

You must be prompted that the third and 4th may reduce the readability of your code, which emphasizes speed and performance, and if you don't want to sacrifice the readability of your code, you might want to skip them.

It is usually good practice to make code simpler and smaller, one of which is to extract the contents of the else narrative sentence, and Christian Montoya has a good example of using shorter else narrative sentences to convert characters.

Common Else statements:

The following is the referenced content:
if (This condition)
{
$x = 5;
}
Else
{
$x = 10;
}

If the default value of $x is 10, we start at 10 without specifying its value in the Else section.

 
 
The following is the referenced content:
$x = 10;
if (This condition)
{
$x = 5;
if (this condition)    { (   $x = 5; $ x = 5;    } )else        { (   $x = ten; $ x = ten;    } )

It doesn't seem like much difference, but if you have many else statements in your program, the cumulative effect will be obvious.

4. Discard those brackets


As with simplifying the else statement, if there is only one expression after the control structure, discarding the parentheses around the expression can save some characters. Evolt.org has a deft example of how to reduce the use of parentheses.

The following is the referenced content:
if ($gollum = = ' halfling ') {
$height--;
}

 This is the same: 
if ($gollum = = ' halfling ') $height--;
This method can be used multiple times in the program:

The following is the referenced content:
if ($gollum = = ' halfling ') $height-;


if ($frodo! = ' dead ')
Echo ' Gosh darnit, Roll again Sauron ';

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


5. Take Str_replace (), ereg_replace () and Preg_replace ()

In terms of efficiency, str_replace () is more suitable than regular expressions to replace strings, and according to some studies, the efficiency of Str_replace () is 61% higher than that of ereg_replace () and Preg_replace ().

  $x = ten; $ x = ten;   if (this condition) if (in this case)    { (    $x = 5; $ x = 5;    } )  
 
 
    • Total 2 Pages:
    • Previous page
    • 1
    • 2
    • Next page

http://www.bkjia.com/PHPjc/364244.html www.bkjia.com true http://www.bkjia.com/PHPjc/364244.html techarticle Fire Station Academy documentation at smashing Magazine's website, Glen Stansberry presents 10 advanced PHP tips that can instantly lift your PHP programming prowess, including SQL injection attacks ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.