1. Embed variables in strings
Do you know you can embed your variable names inside a string? Not just simple variables but any variables, like an array or an object. All you need to do is warp it with {}. See PHP String reference for more info
Show Code
$s = "hello";
$a['ele1'] = 'world';
$obj->ele1 = '!';
echo ("$s {$a['ele1']} {$obj->ele1}";
Note: this technique have a performance issue, if performance is a critical issue on your server, then DON’T use it.
2. Use shortform of conditional assignment
Guys like me, who migrated from Delphi or other programming languages, might not know or familar with this. Basically an variable assignment inside an if statement can be shorten using ? and :
Here’s an example:
Show Code
if ($a == 1) {
$a = 0;
} else {
$a = 1;
}
can be shorten to:
Show Code
“?” tests if $a == 1 then assign any after “?”, it can be number, string, array or object, otherwise assign the value after “:”.
3. Swapping of variables - the short way
If you want to swap two variables this onliner is really handy.
Show Code
You can even do more with the construct list() = array().
Show Code
list($b,
$c,
$a) =
array($a*
$b,
$b*
2,
$c*
$a);
Each element of the array gets evaluated prior being assigned to the elements
of the list!
Thus the variables $a, $b and $c do not change until the expressions are
evaluated.
To achieve the same result without the use of list() = array()you need at least
one helper variable.
Ref: http://www.zend.com/tips/tips.php?id=196&single=1
4. Use get_defined_functions() and get_defined_constants()
Sometimes you downloaded some code from the Internet, you know what it does, but you don’t how it does it, and there is no documentation. You end up reading hundreds of lines of codes, just to find what that variable is called, and what is the name of that function you were looking for.
Here is quick method. You can create a dummy php file contains the following:
Show Code
// Don't forget to include what ever php file you want to debug
The print out is still a bit messy, but at least you won’t have to look through all the files.
5. Use date(”r”) for RFC date
If you ever need to encode a date in RFC format for a RSS feed or other XML files, don’t spend time look for how you can encode using date format strings, there is already one at almost end of the list. its date(”r”);
- c - ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
- r - RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
- U - Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
6. Never write duplicated code
I am sure for any programmers, none will like to write the same code over and over again. But I know there are still many Copy & Paste addicts out there. Here is what you should do.
If you have a block of common code, that gets uses at least twice, make it a function.
If you have more than two files using the same function, make it a seperate file, include that file.
If you have more than two shared files, use another file to include them all, so you include one file rather a dozen.
7. Finally, when you are in trouble, remember “echo” is your best friend.
If you ever run into a situation where your code is not working as intended. Sure you can look through the code and work it out in mind what’s wrong with it. But I always find echo things out is best way to debug, and there are also print_r for arrays, var_export.
Phew! I started the topic with 7 tips, but ran out of ideas from tip #2. I knew I should have started with lower number. But at last I finished the topic with some help from php.net and zend.com. I hope this helpful.