Thursday, March 29, 2007

Tuesday, March 27, 2007

How to add keyboard shortcuts to your website

By Justin Silverton

The following javascript code will allow you to add keyboard shortcuts to any webpage.

The code

(put this on any page where you want keyboard shortcuts)

I can't put the HTML code please refer:- Html Code

(put this in a file called shortcut.js and upload to the same directory as the webpage with the above code). This example will display an alert message when the escape key is pressed.

function keyShortcut() {
var e = window.event;
var code = e.keyCode;
if (code == 112) { //checks for the escape key
alert('escape key pressed');

The following are some more keyboard codes that can be used within the above script (in the if code == X, where X is one of the codes below).

key Code
tab 9
enter 13
leftwindow key 91
right window key 92
f1 112
f2 113
f3 114
f4 115
f5 116
f6 117
f7 118
f8 119
f9 120
f10 121
f11 122
f12 123

Saturday, March 17, 2007

PHP Optimization Tricks

There are a number of tricks that you can use to squeeze the last bit of performance from your scripts. These tricks won't make your applications much faster, but can give you that little edge in performance you may be looking for.

1.When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't.

2.When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. But Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.

if (strlen($foo) <> 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1);
if (isset($keys['mangoes'])) { ... }

The bottom search mechanism is roughly 3 times faster.

9.file including and requiring.

First, you should only use require if you KNOW you will need that file on that page. If you MIGHT use it, use
include instead because PHP opens up all files that are required, whether the script gets there or not. To help out the programmer,
I suggest using "include_once" and "require_once".

Second, get your source file sizes as small as possible and only include/require files you absolutley must have.
If you only need one function in a file, consider breaking that function into its own file. I found on my system
that for every KB of source code that was included/required, I lost one transaction per second (tps). This may
not lead to perfectly organized code, but if speed is a concern, it is definitley worth it.

10.true is faster than TRUE
How come true is faster than TRUE?
This is because when looking for constants PHP does a hash lookup for name as is. And since names are always stored lowercased, by using them you avoid 2 hash lookups.

11.Perl-regexps are faster than POSIX-regexps This books explains regex and why a posix regex is slower.

just out of curiosity I wanted to see which one of these structures are faster. I believed that "for" will be the fastest but the tests proved I was wrong.

for: 2.078712940216064
while: 1.981828927993774
do: 1.936743974685669

For 2,000,000 iterations the do-while structure is almost 0.15 seconds faster than the for structure. This won't mean very much for small sites but in complex application where you'll have to handle many hits it will make a difference.

13.Here are two tips for loops. If the number of iterations in a loop is low, you might get some performance gain from replacing the loop with a number of statements. For example, consider a for loop that sets 10 values in an array. You can replace the loop with 10 statements, which is a duplication of code, but may execute slightly faster.

Also, don't recompute values inside a loop ex:- count(arrname) .The price for coding in this style was a slight hit in performance, as the loop calls the count function repeatedly.

If you know or have any additional optimization tricks let me know.


How to use MySQL REPLACE function

Here’s a MySQL query fuction that’s easy to forget about: REPLACE. Let’s say I have a bunch of records in a field with http://www and want to quickly change all to http://. Here’s the syntax to update:

UPDATE tdurl_1 SET URL = REPLACE(URL, ‘’,'’);