Ten Tips to Improve Your PHP Programming Level_PHP Tutorial

WBOY
Release: 2016-07-21 14:56:40
Original
992 people have browsed it

Since PHP was born in 1995, it has grown rapidly. Since then, PHP has become the most popular programming language for web applications. Many popular websites are powered by PHP, and most scripts and web programs are written in this popular language.
Due to the popularity of PHP, it is almost impossible for web developers to not know a little bit of PHP knowledge. This tutorial is aimed at those who have just experienced the beginning stages of PHP and are ready to roll up their sleeves and dive into the language. Listed below are ten excellent techniques that PHP developers should learn and use every time they program. These experiences accelerate developer proficiency and make code more perceptible, cleaner, and more optimized for code execution.
1. Use a SQL injection attack table A list of common SQL injections.
SQL injection attack is a sinister behavior. SQL injection attack is a security vulnerability exploit that allows hackers to exploit weaknesses in the code to enter your database. . Although this article is not MySQL related, many PHP programmers use the MySQL database, so it is easy to learn how to avoid (SQL injection) if you want to write safe code.
Furuh Mavituna has a good SQL injection cheat sheet, which has a section on the weaknesses of PHP and Mysql programming. If you can avoid the habits pointed out in this cheat sheet, your code will become less susceptible to scripting attacks.
2. Learn the differences between comparison operators PHP's list of comparison operators.
Comparison operators are a huge part of PHP, and many programmers are not as aware of the differences between them as they should be. skilled. In fact, an article in the 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 should be immediately recognized for common functions such as strpos(). Since zero in PHP is analogous to FALSE it means that without this operator there would 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. Obviously this has many applications elsewhere where returning zero is not equivalent to FALSE.
You must understand that == represents equality and === represents consistency. You can read a list of the comparison operators on the PHP.net website.
3 To shorten the else statement, it should be stated that items 3 and 4 both make the code less readable. These two items emphasize speed and execution. If you choose not to sacrifice readability, you can skip these two items.
Anything that can make your code simpler and smaller is generally a good practice. The purpose of this article is to take the "middleman" out of the else statement, so to speak. Christian Montoya has a great example of using short else statements to reduce characters.
General else statement
[code language="php"]
if( this condition )
{
$x = 5;
}
else
{
$x = 10;
}
[/code]
If $x defaults to 10, just initialize it to 10. There is no need to go through the trouble of typing the else part.
[code language="php"]
$x = 10;
if( this condition )
{
$x = 5;
}
[/code]
There doesn’t seem to be much difference in code space saving. If there are many else statements in your program, this will be obviously different.
4. Dropping brackets saves space and time in your code.
Just like when writing an else statement, you can also omit it in an expression immediately following a control statement. brackets to save some characters. Evolt.org has a simple example listing a structure that omits the brackets
[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 perspective See, str_replace() is more efficient than regular expressions in replacing strings. 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 regular expressions, ereg_replace() and preg_replace() will be much faster than str_replace().
6. Use the ternary operator Consider using the ternary operator instead of using if/else statements entirely. PHP Value gives a very good example of what the 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 cluttered and easier to browse. Note. Don't use more than one ternary operator in an expression statement, because PHP doesn't always know what to do in this situation.
7 memcachedMemcached is an excellent database caching system to use with PHP.
While there are many caching solutions to choose from, Memcached ranks at the top as the most efficient database cache. It is not the easiest caching system to implement, but if you use PHP to build a website using a database, Memcached can definitely speed up your website. The caching system Memcached was first built for the blog website LiveJournal.
PHP.net has an excellent tutorial on 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 a PHP framework in every project of yours, but frameworks like CakePHP, Zend, Symfony and CodeIgniter can greatly reduce the time you spend building a website. Frameworks can be used to help reduce the overhead of developing web applications and web services by wrapping commonly used mechanisms. If you can take care of the repetitive work when writing a website, you can. Develop faster. The less code you write, the less time you have to debug and debug.
9. Correct use of the error suppression operator The error suppression operator (or error control operator in the PHP manual) is the @ symbol. When placed in front of a statement in PHP, it simply tells the program Do not (this is now in the original text, probably a clerical error by the original author) display any errors caused by this statement. This operator is useful if you are unsure about the value or don't want to throw any errors.
However, many programmers use error suppression operators incorrectly. If you keep efficiency in mind when writing code, the @ operator is very slow and expensive to run.
Michel Fortin has some examples of how to use other methods to circumvent the @ error suppression operator. This is a way he uses the isset function to replace the error printing operator.
[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 about twice as slow . A good solution is to assign the variable by reference so that no warnings are 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 places that require higher efficiency and will not be affected.
10. Use isset instead of strlen Switching isset for strlen makes calls about five times faster.
If you are checking the length of a string, use isset instead of strlen. By using isset, your calls will be five times faster. It's important to point out that by using isset, your call will work if the variable doesn't exist.
D-talk has an example on 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 like the techniques mentioned today, This all adds up to fast, clean code.
[via 10 Advanced PHP Tips To Improve Your Programming]

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364195.htmlTechArticleSince PHP was born in 1995, it has grown rapidly. Since then, PHP has become the most popular programming language for web applications. Many popular websites are powered by PHP, and most scripts and web programs...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template