10 tips to improve your PHP programming level _php Tutorial

Source: Internet
Author: User
Tags php and mysql sql injection attack sql injection cheat sheet
Since the birth of PHP in 1995, the rapid 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 Web programs are written in this popular language.
Due to the prevalence of PHP, it is almost impossible for web developers to understand a little bit of PHP knowledge. This tutorial is for those who have just gone through the initial stages of PHP and are ready to roll up their sleeves and go deep into the language. Below is a list of the 10 best technologies that PHP developers should learn and use every time they program. These experiences can speed up the developer's mastery and make the code easier to perceive, tidy, and optimized for code execution.
1. Use a SQL injection attack table A list of common SQL injections.
SQL injection attacks are a sinister behavior, and SQL injection attacks are a security exploit that allows hackers to exploit the weaknesses of the code into your database. Although this article is not MySQL-related, many PHP programmers are using MySQL database, so it is easy to learn how to avoid (SQL injection) if you want to write secure code.
Furruh Mavituna has a good SQL injection cheat sheet, which is partly about the weaknesses of PHP and MySQL programming. If you can avoid the habit of this cheat sheet point out, your code will become less prone to scripting attacks.
2. Learn the differences between the operators of PHP ' s list of comparison operators.
Comparison operators are a huge part of PHP, and many programmers can't be as skilled as they should be in the difference between them. In fact, an I/O reader article shows that many PHP programmers do not correctly tell the difference between comparison operators. Tsk Tsk.
These is extremely useful and most phpers can ' t tell the difference between = = and = = =. Essentially, = = looks for equality, and by that PHP would generally try to coerce data into similar formats, eg:1 = = ' 1′ ( true), whereas = = = looks for identity:1 = = = ' 1′ (false). The usefulness of these operators should is immediately recognized for common functions such as Strpos (). Since Zero in PHP are analogous to FALSE it means, without this operator there would be no-to-tell from the result of Strpos () if something is in the beginning of a string or if Strpos () failed to find anything. Obviously this have many applications elsewhere where returning zero is not equivalent to FALSE.
To understand, = = equals, = = = is consistent. You can read an article on the list of the comparison operators on the Php.net website.
3 cut short The Else statement requires that both 3rd and 4th have a somewhat reduced readability of the code, and these two articles emphasize speed and execution. If you choose not to sacrifice readability, then you can skip these two lines.
Anything that makes the code simpler and smaller is usually a good habit. The purpose of this article is to take the "middleman" out of the else statement so as to speak. Christian Montoya has a very good example of reducing characters with short else statements.
The general Else statement
[Code language= "PHP"]
if (This condition)
{
$x = 5;
}
Else
{
$x = 10;
}
[/code]
If the $x default is 10, initialize it to 10. There is no need to enter the else part of the trouble again.
[Code language= "PHP"]
$x = 10;
if (This condition)
{
$x = 5;
}
[/code]
There seems to be no significant difference in the space savings of the code, which can be significantly different if there are many else statements in your program.
4. Omit the brackets dropping brackets saves space and time in your code.
As in writing the Else statement, you can save some characters by omitting the parentheses in the expression immediately following a control statement. Evolt.org has a simple example listing the structure of an elliptical bracket
[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! = ' dead ')
Echo ' Gosh darnit, Roll again Sauron ';
foreach ($kill as $count)
Echo ' Legolas strikes again, that makes '. $count. ' For me! ';
[/code]
5 Select Str_replace instead of Ereg_replace and preg_replacespeed tests show that Str_replace () is 61% faster.
From an efficiency standpoint, the str_replace () singular expression is more efficient in terms of replacing strings. In fact, according to making the Web, the Str_replace () is 61% more efficient than regular expressions like ereg_replace () and Preg_replace ().
If you are using regular expressions, ereg_replace () and Preg_replace () will be much faster than str_replace ().
6. Use the ternary operator to consider using the ternary operator instead of using the If/else statement entirely. PHP value gives a very good example of what a ternary operator is.
[Code http://www.yeeyan.com/articles/tag/php "Target=_blank $included =" 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 ternary operator saves your line space and makes your code less confusing and easy to navigate. Be careful 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 a excellent database caching system to use with PHP.
While there are many caching options to choose from, Memcached is among the best in the most efficient database caches. It's not the simplest caching system to perform, but if you use PHP to build a Web site using a database, memcached will definitely be able to speed up your site. Memcached This caching system for the first time is to livejournal this blog site and built.
Php.net has an excellent tutorial to show you how to install and use memcached in your project.
8. Use a framework cakephp is one of the top PHP frameworks.
You may not be able to use PHP frameworks in each of your projects, but like CakePHP, Zend, Symfony and CodeIgniter, these frameworks can greatly reduce the time you build a website. A framework is a software that improves the speed of development by packaging a common mechanism. Frameworks can be used to help reduce the overhead of developing network applications and network services.
If you are working on a Web site that you can manage over and over again through the framework, you will develop at a higher speed. The less code you write, the less debug and error-Debugging.
9. The correct use of the suppress error operator error suppression operator (or in the PHP manual called the error Control) is the @ symbol, when placed in PHP in front of a statement, it simply tells the program not (now, estimated as the original author clerical error) to show any errors generated by this statement. This operator is useful if you are unsure of the value or do not want to throw any errors.
However, many programmers mistakenly use the error suppressor operator. This @ operator is very slow and expensive to run if you keep your running efficiency in mind when you write code.
Michel Fortin has some examples of how to circumvent the @ error suppression operator in other ways. This is a way for him to replace the error printing operator with the Isset function.
[Code language= "PHP"]
if (Isset ($albus)) $albert = $albus;
else $albert = NULL;
[/code]
Equivalent to:
[Code language= "PHP"]
$albert = @ $albus;
[/code]
But although the second method is more organized, it runs twice times slower. A good workaround is to assign variables by reference so that no warnings are triggered, such as:
[Code language= "PHP"]
$albert = & $albus;
[/code]
It should be noted that these changes may have unintended side effects and should be used in areas that are not affected by higher efficiency requirements.
10. Use isset instead of strlenswitching isset for Strlen makes calls about 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 five times times faster. It should be noted that by using Isset, your call will be valid if the variable does not exist.
D-talk has an example that shows how to swap out isset for strlen:
A while ago I had a discussion about the optimal-to-determine a string length in PHP. The obvious-to-use strlen ().
However to check the length of a minimal requirement it's actually not this optimal to use strlen. The following is actually much faster (roughly 5 times)
This is only a small change, but as today's tips add up to a quick, clean code.
[via Advanced PHP Tips to Improve Your programming]

http://www.bkjia.com/PHPjc/364195.html www.bkjia.com true http://www.bkjia.com/PHPjc/364195.html techarticle since the birth of PHP in 1995, the rapid growth. Since then, PHP has become the most popular programming language in Web applications. Many popular websites are powered by PHP, and most of the scripts and Web threads ...

  • Related Article

    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.