Copyright statement: original works can be reproduced. During reprinting, you must mark the original publication, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://blog.csdn.net/mayongzhan-ma yongzhan, myz, mayongzhan
Address: http://www.alexatnet.com/node/100
PHP is a good language and has many surprises. Today I have seen an interesting method in the blog of Arnold Daniels. He talked about temporary variables in PHP. This secret is beneficial to "lazy" programmers, so that programmers do not have to think about the name of the variable. They can use the variable name ${0 }.
I am more lazy than Arnold Daniels and don't want to use variables at all. Below are some tips to reduce your code.
1. Use | (OR) and & (and) operations to replace if.
// Standard writing
$ Status = fwrite ($ H, 'some text ');
If (! $ Status ){
Log ('writing failed ');
}
// Less code
$ {0} = fwrite ($ H, 'some text ');
If (! $ {0}) log ('writing failed ');
// Less code
Fwrite ($ H, 'some text') or log ('writing failed ');
2. Use the ternary operator.
// Standard writing
If ($ age <16 ){
$ Message = 'Welcome! ';
} Else {
$ Message = 'you are too old! ';
}
// Less code
$ Message = 'you are too old! ';
If ($ age <16 ){
$ Message = 'Welcome! ';
}
// Less code
$ Message = ($ age <16 )? 'Welcome! ':' You are too old! ';
3. Replace the while.
// Standard writing
$ I = 0;
While ($ I <100 ){
$ Source [] = $ target [$ I];
$ I + = 2;
}
// Less code
For ($ I = 0; $ I <100; $ source [] = $ target [$ I + = 2]);
4. variables must be written in many places. For example, PHP fluent API tips. For example, you can call a function to obtain an array and then directly use the array element.
// In the following example, an error occurs. Because the returned array does not assign a value to a variable, the ['extension'] is used directly.
$ Ext = pathinfo('file.png ') ['extension'];
// Result: Parse error: syntax error, unexpected '[' in... on line...
You can create a function to solve this problem, as shown in the following code: (a pretty good method. It looks awkward ...)
// Returns reference to the created object
Function & R ($ v) {return $ V ;}
// Returns array offset
Function & A (& $ A, $ I) {return $ A [$ I];}
5. Spend more time studying the built-in Function Methods of PHP. There are many interesting methods in PHP to make your code shorter.
6. Do not be lazy when writing more code can make the program clearer. Spend more time writing comments and try to write readable code. This is the real time-saving technique. (Write more comments and easy-to-read code, saving time in future debugging modifications)
PHP is a good language, but there are always surprises. and today I 've seen an interesting approach in Arnold Daniels's blog. he talks about temporary variables in PHP. this tip is useful to "lazy" developers who do not even think about variable names. they may prefer magic names like $ {0} and 0 is good enough variable name, why not...
But I'm even more lazy then Arnold and sure that when there is no variable, then there is no problem. So here are a few tips that can make your code shorter and harder to read
1. Use | (OR) and & (and) Operations instead of if.
// A lot of code
$ Status = fwrite ($ H, 'some text ');
If (! $ Status ){
Log ('writing failed ');
}
// Less code
$ {0} = fwrite ($ H, 'some text ');
If (! $ {0}) log ('writing failed ');
// Even less code
Fwrite ($ H, 'some text') or log ('writing failed ');
2. Use ternary operator.
// A lot of code
If ($ age <16 ){
$ Message = 'Welcome! ';
} Else {
$ Message = 'you are too old! ';
}
// Less code
$ Message = 'you are too old! ';
If ($ age <16 ){
$ Message = 'Welcome! ';
}
// Even less code
$ Message = ($ age <16 )? 'Welcome! ':' You are too old! ';
3. Use for instead of while.
// A lot of code
$ I = 0;
While ($ I <100 ){
$ Source [] = $ target [$ I];
$ I + = 2;
}
// Less code
For ($ I = 0; $ I <100; $ source [] = $ target [$ I + = 2]);
4. In some cases PHP requires you to create a variable. For example, Ech the PHP fluent API tips article. Another example is getting array element when array is returned by the function.
$ Ext = pathinfo('file.png ') ['extension'];
// Result: Parse error: syntax error, unexpected '[' in... on line...
To handle all these situation you can create a set of small functions which shortcuts frequently used operations.
// Returns reference to the created object
Function & R ($ v) {return $ V ;}
// Returns array offset
Function & A (& $ A, $ I) {return $ A [$ I];}
5. Please e the language you use. php is very powerful and has a lot of functions and interesting aspects of the language which can make your code more efficient and short.
6. when it is better to write more and then read the code easily, do not be lazy. spend a few seconds and write a comment and more readable construction. this is only a tip in this list that really can save hours, not minutes.