Ten tips to improve your PHP programming skills _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags sql injection attack sql injection cheat sheet
Ten tips to improve your PHP programming skills. PHP has grown rapidly since its birth in 1995. Since then, php has become the most popular programming language in web applications. Many popular websites are driven by php, and most scripts and network processes have grown rapidly since the birth of PHP in 1995. Since then, php has become the most popular programming language in web applications. Many popular websites are driven by php, and most scripts and network programs are written in this popular language.
Due to the prevalence of PHP, it is almost impossible for network developers to understand PHP knowledge. This tutorial is intended for those who have gone through the initial stage of PHP and are ready to roll up their sleeves and go deep into the language. The following lists the top 10 technologies that PHP developers should learn and use in each programming. These experiences can accelerate developers' mastery and make the code more perceptible, clean, and optimized for code execution.
1. use an SQL injection attack table A list of common SQL injections.
SQL injection attacks are a sinister behavior. SQL injection attacks are a security vulnerability that allows hackers to exploit code vulnerabilities to access your database. Although this article is not related to Mysql, many PHP programmers use Mysql databases. Therefore, if you want to write secure code, it is easy to learn how to avoid (SQL injection.
Furruh Mavituna has a good SQL injection cheat sheet, which has some weaknesses in PHP and Mysql programming. If you can avoid this cheat sheet habit, your code will become less vulnerable to script attacks.
2. learn the differences between comparison operators in PHP's list of comparison operators.
Comparison operators are a huge part of PHP, and many programmers are not as skilled in the differences between them as they should be. In fact, an article in I/O reader shows that many PHP programmers cannot correctly tell the difference between comparison operators. Tsk tsk.
These are extremely useful and most PHPers can't tell the difference between = and =. essentially, = looks for equality, and by that PHP will generally try to coerce data into similar formats, eg: 1 = '1' (true ), whereas === looks for identity: 1 === '1' (false ). the usefulness of these operators shoshould be immediately recognized for common functions such as strpos (). since zero in PHP is analogous to FALSE it means that without this operator there wocould be no way to tell from the result of strpos () if something is at the beginning of a string or if strpos () failed to find anything. obviusly this has applications elsewhere where returning zero is not equivalent to FALSE.
To understand, = indicates the same, = indicates the same. You can read a list of the comparison operators article on the PHP.net website.
3. the 3rd and 4th else statements must be declared to reduce the readability of the code. The two statements emphasize speed and execution. If you choose not to sacrifice readability, you can skip the two.
Anything that makes the code simpler and smaller is usually a good habit. The purpose of this article is to take the man in the middle out of the else statement, so to speak. Christian Montoya has a very good example of using short else statements to reduce characters.
General else statements
[Code language = "php"]
If (this condition)
{
$ X = 5;
}
Else
{
$ X = 10;
}
[/Code]
If $ x defaults to 10, you can initialize it to 10. There is no need to enter the else part again.
[Code language = "php"]
$ X = 10;
If (this condition)
{
$ X = 5;
}
[/Code]
It seems that there is not much difference in code space savings. if your program has many else statements, this will be significantly different.
4. omit the brackets Dropping brackets saves space and time in your code.
Just like when writing an else statement, you can save some characters by omitting the parentheses in the expression that follows the control statement. Evolt.org has a simple example listing a structure with parentheses omitted.
[Code language = "php"]
If ($ gollum = 'halfling '){
$ Height --;
}
[/Code]
This is the same as the following:
[Code language = "php"]
If ($ gollum = 'halfling') $ height --;
[/Code]
You can even use it in complex situations.
[Code language = "php"]
If ($ gollum = 'halfling') $ height --;
Else $ height ++;
If ($ frodo! = 'Dest ')
Echo 'Gosh darnit, roll again Sauron ';
Foreach ($ kill as $ count)
Echo 'legolas strikes again, that MAK'. $ count. 'for me! ';
[/Code]
5 Select str_replace instead of ereg_replace and preg_replaceSpeed tests show that str_replace () is 61% faster.
From the perspective of efficiency, str_replace () is more efficient than regular expressions in string replacement. In fact, according to Making the Web, str_replace () is 61% more efficient than regular expressions like ereg_replace () and preg_replace.
If you are using a regular expression, ereg_replace () and preg_replace () are much faster than str_replace.
6. use the ternary operator instead of the if/else statement. PHP Value provides a very good example to illustrate what the ternary operator is.
[Code http://www.yeeyan.com/articles/tag/php "target = _ blank $ encoded deD =" null "> php
// PHP COde Example usage for: Ternary Operator
$ Todo = (empty ($ _ POST ['Todo '])? 'Default': $ _ POST ['Todo '];
// The above is identical to this if/else statement
If (empty ($ _ POST ['Todo ']) {
$ Action = 'default ';
} Else {
$ Action = $ _ POST ['Todo '];
}
?>
[/Code]
The three-element operator saves your row space and makes your code not so messy and convenient for browsing. Be sure not to use more than one ternary operator in an expression statement, because PHP does not always know what to do in this case.
7 memcachedMemcached is an excellent database caching system to use with PHP.
Although there are many cache solutions to choose from, Memcached is the most efficient database cache. It is not the simplest cache system to execute, but if you use php to build a website and use a database, Memcached will certainly be able to accelerate your website. For the first time, the Memcached cache system was built for the blog website LiveJournal.
PHP.net has an excellent tutorial on installing and using memcached in your project.
8. Use a framework CakePHP is one of the top PHP frameworks.
You may not be able to use the PHP framework in each of your projects, but frameworks like CakePHP, Zend, Symfony and CodeIgniter can greatly reduce the time you need to build a website. The framework is a software that increases the development speed by packaging common mechanisms. The framework can be used to reduce the overhead of developing network applications and network services.
If you can use the framework to manage repeated tasks when writing a website, you will develop the website at a higher speed. The less code you write, the less debugging and debugging are performed.
9. correct use of the suppression error operator error suppression operator (or the error control operator in the php Manual) is the @ symbol. when a statement is placed in front of a PHP statement, it simply tells the program not to (now in the original text, which is estimated to be a written mistake by the original author) display any errors caused by this statement. This operator is useful if you are not sure about this value or do not want to throw any errors.
However, many programmers mistakenly use the error suppression operator. If you keep the running efficiency in mind when writing code, this @ operator is very slow and costly to run.
There are some examples of michelfortin to illustrate how to use other methods to avoid the @ error suppression operator. This is a method in which he uses the isset function to replace the incorrect printing operator.
[Code language = "php"]
If (isset ($ albus) $ albert = $ albus;
Else $ albert = NULL;
[/Code]
Equivalent:
[Code language = "php"]
$ Albert =@$ albus;
[/Code]
However, although the second method is more organized, the running speed is about two times slower. A good solution is to allocate variables by referencing them so that no warning is triggered, for example:
[Code language = "php"]
$ Albert = & $ albus;
[/Code]
It should be noted that these changes may have some unexpected side effects and should be used in areas with high efficiency requirements that will not be affected.
10. use isset instead of strlenSwitching isset for strlen makes callabout five times faster.
If you are preparing to check the length of a string, use isset instead of strlen. By using isset, your call will be faster than five times. It should be noted that by using isset, your call will also be valid if the variable does not exist.
D-talk has an example to illustrate how to swap out isset for strlen:
A while ago I had a discussion about the optimal way to determine a string length in PHP. The obvious way is to use strlen ().
However to check the length of a minimal requirement it's actually not that optimal to use strlen. The following is actually much faster (roughly 5 times)
This is just a small change, but the techniques mentioned today will add up to achieve a fast and clean code.
[Via 10 Advanced PHP Tips To Improve Your Programming]

The birth of 1995 is a fast growth. Since then, php has become the most popular programming language in web applications. Many popular websites are driven by php, and most scripts and network processes...

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.