December 21st, 2007 |
No Comments
A couple of days ago a read through my blog, and discovered that I too suffer from the Curse of Knowledge described in the book Made To Stick
.
I tend to write sloppy, knowing that the readers already knows some of this stuff. But this is wrong. The Curse of Knowledge means exactly this. You want to present something to people, but you didn’t take in consideration that people doesn’t have similar background as you do, which makes you a bad presenter.
Chip & Dan Heath’s book is absolutely great. A must read for all people, not just programmers, but managers, presenters, teachers. The book defines and teaches how to present something to people that they understand exactly the same as you intended to.
This was the first book that I enjoyed reading every single page.

Image copyright Amazon.com
December 21st, 2007 |
No Comments
It’s a fact that search engines like Google, Yahoo and MSN, but mainly Google ranks down websites with duplicate content.
Duplicate content mostly occurs, because of the misconfiguration of canonical URLs.
A canonical URL is the single URL that you want your site to be known by and accessed on the Internet.
Consider the following URLs:
www.example.com
example.com
These two addresses leads to the same website. Google and other search engines crawls websites by URLs so think about it: Google goes to www.example.com, finds content, indexes it, goes to example.com and finds the same content, it ranks down your page.
So what can you do about it? With a little .htaccess rewrite rules it can be fixed:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^primalskill\.com [NC]
RewriteRule ^(.*) http://www.primalskill.com/$1 [R=301]
As you can see if you access my website with primalskill.com it will redirect it www.primalskill.com. The [R=301] part means permanently redirect, with this you’re letting know search spiders/crawlers that the accessed site is permanently moved to the new location, so change the indexing to the new location. This is very important.
You can check this out. Try accessing http://primalskill.com
December 20th, 2007 |
3 Comments
Check out my other PHP optimization tips by clicking on the Optimization category.
1. Don’t use complex if…else statements. If you want to evaluate concrete values rather then logical ones than it’s recommended to use the switch control structure instead.
Ex.
We want to evaluate if an integer has the values of 200 or 404.
Bad method:
//$i is the integer
if ($i == 200)
{
//do something
}
elseif ($i == 404)
{
//do something
}
else
{
//none of the values, do something accordingly.
}
Good method:
//$i is the integer
switch ($i)
{
case 200:
{
//do something
break;
}
case 404:
{
//do something
break;
}
default:
{
//none of the values, do something accordingly.
break;
}
}
I admit it’s a little bit more coding this way, but it’s much more readable.
2. Try to simplify if…else statements.
Ex.
We want to evaluate an integer if it has the value of 500 and return true or false accordingly. I will write this in a function just to be more exact.
Bad method:
function bad_method()
{
if ($i == 500)
{
return true;
}
else
{
return false;
}
}
You can simplify this by dropping the else part and use the return keyword only once. This way your code will be even more readable. In my experience head developers really don’t like more than one return in a function (method).
Good method:
function good_method()
{
$return_value = false;
if ($i == 500)
{
$return_value = true;
}
return $return_value;
}
December 19th, 2007 |
No Comments
I couple if days ago I had to update a rich text editor type of script for a client. The client only needed basic functionality like font styling: bold, italic, underline, text justify and font size.
The initial text editor was a huge script around 300KB, full of features like image editing, style save, table creation, etc. Now the problem with this script is it’s size, it takes time to load and the possibility of errors are higher. It’s feature creep.
First I started to edit the script, stripping out all the unnecessary features, but I ran into some errors so I dropped the whole script and searched for another one… and another one… and another one. I found that all widely used text editor scripts are feature creep, big and slow.
Until I found NicEdit. This is a must have for all developers, it’s small <30 KB, it’s 10KB compressed. It has all the features for basic text editing, and that’s just enough. You should check it out at http://nicedit.com
All this thanks to Brian Kirchoff
Keep up the good work!
(This is not a paid post)
December 19th, 2007 |
2 Comments
Check out my other PHP optimization tips by clicking on the Optimization category.
Using the for loop break and continue keywords. Picture this: you have to search a value in an array or 50000 elements, you iterate through the array and find your value for ex. at the 1000th element and echo out an ok message. I will demonstrate how to optimize this scenario.
Method 1:
//$a is an array of 50000 elements
$result = 'not ok';
for ($i=0, $cnt=count($i); $i<$cnt; $i++)
{
if ($a[$i] == 1000)
{
$result = 'ok';
}
}
echo $result;
The problem with the above code is that it will iterate thourgh the array even if the value is found. To correct this we have to add an extra break keyword in the if statement. This will exit from the for loop if the value is found. Check out the code below.
Method 2:
//$a is an array of 50000 elements
$result = 'not ok';
for ($i=0, $cnt=count($i); $i<$cnt; $i++)
{
if ($a[$i] == 1000)
{
$result = 'ok';
break;
}
}
echo $result;
Results:
|
Time |
Memory Usage |
| Method 1 |
0.08406 |
112 |
| Method 2 |
0.00262 |
112 |
We don’t have to deal with memory usage here, because the 2 method uses the same amount of memory, the speed is what we’re after. The second method performs much better than the first. It’s a good practice to use this technique on busy websites.
Also you should check out the continue keyword too.
December 18th, 2007 |
2 Comments
Check out my other PHP optimization tips by clicking on the Optimization category.
The speed and memory usage of inline code, functions and classes
Method 1:
$a = 1000;
Method 2:
function method2()
{
$a = 1000;
}
Method 3:
class test
{
function method3()
{
$a = 1000;
}
}
Method 4:
class test
{
function __construct()
{
$a = 1000;
}
}
Results:
|
Time |
Memory Usage |
| Method 1 |
0.00721 |
48 |
| Method 2 |
2.19345 |
96 |
| Method 3 |
4.10079 |
240 |
| Method 4 |
2.88486 |
192 |
Ok. This is a question of preference what coding style we’re using. I choose the object oriented approach
It’s the best considering manageability, in this case I choose it over speed and memory usage. Method 4 is slightly faster and consumes less memory, because it avoids the creation of another extra function, instead it creates the variable in the constructor method.
December 18th, 2007 |
1 Comment
Check out my other PHP optimization tips by clicking on the Optimization category.
I will present the speed and memory usage of 3 loops: for, foreach and while
Method 1:
//$a is an array with 50000 elements.
$max = count($a);
for ($i=0;$i<$max; $i++)
{
//do something
}
Method 2:
//$a is an array with 50000 elements.
foreach ($a as $key => $value)
{
//do something
}
Method 3:
//$a is an array with 50000 elements.
$max = count($a);
while ($a<$max)
{
//do something
}
Results:
|
Time |
Memory Usage |
| Method 1 |
0.03986 |
96 |
| Method 2 |
0.05441 |
96 |
| Method 3 |
0.02038 |
96 |
As you can see all 3 methods use the same memory. The slowest is Method 2 the foreach loop and the fastest is Method 3.
December 17th, 2007 |
No Comments
Check out my other PHP optimization tips by clicking on the Optimization category.
Using echo in for loops is slow instead use a variable to store the results and echo it after the for loop finishes. That’s a myth.
Method 1:
//$a is an array with 10000 elements.
$max = count($a);
for ($i=0;$i<$max; $i++)
{
echo $a[$i];
}
Method 2:
//$a is an array with 10000 elements.
$max = count($a);
$store = '';
for ($i=0;$i<$max; $i++)
{
$store .= $a[$i];
}
echo $store;
Results:
|
Time |
Memory Usage |
| Method 1 |
0.03499 |
272 |
| Method 2 |
0.035693 |
72656 |
We can say that the time is the same in each method, but on the memory usage we have a huge difference. This is obvious, because we’re storing the array results in the variable. If you want to write the content of an array or anything and you don’t want to store it or modify it, then Method 2 is the good approach.
December 16th, 2007 |
3 Comments
Check out my other PHP optimization tips by clicking on the Optimization category.
Counting an array in the for loop statement is a bad idea, because in the for loop the array’s size should be constant, which means we don’t have to recount the array in every iteration.
Method 1
//$arr is an array omitted for code simplicity.
for ($i=0; $i<count($arr); $i++)
{
//do something
}
Method 2:
for ($i=0, $max=count($arr); $i<$max; $i++)
{
//do something
}
Results:
|
Time |
Memory Usage |
| Method 1 |
0.58728 |
24 |
| Method 2 |
0.06482 |
96 |
Now this result could be tricky, because you can see that Method 1 is performed slower, but used less memory. These results could be interpreted differently.
I always favor speed instead of memory, therefore I’m using Method 2, but the most important thing is you should know the hardware and it’s capabilities from which your script is running…
In the case of Method 2 the additional memory usage is, because PHP needs to allocate memory for the creation of the $max variable. It is still better than counting the array over and over again.
December 16th, 2007 |
No Comments
Check out my other PHP optimization tips by clicking on the Optimization category.
This is my second post on PHP code optimization. First of all, and this is the most important thing, when we talk about PHP code performance we’re not necessarily think of how fast is our code. Performance is relative, it’s a compromise between speed and scalability.
So when I will post my code optimization tips and results I will present them from different perspectives. The first one is obviously speed, and the second is memory usage. Logically the most optimal code is what achieves the best performance in all categories.
A very good explanation can be found at phpLens