Sang Lê Thanh
Senior Drupal Developer
for, foreach, while
#Worst than foreach and while loop
for($i =0; $i < count($array);$i++){
echo 'This is bad, my friend';
}
#Better than foreach and while loop
$total = (int)count($array);
for($i =0; $i < $total;$i++){
echo 'This is great, my friend';
}
' is faster than "
$i++;
$++i
$i+=1;
$i = $i + 1;
Pre-increment where possible
Note that both the Zend Optimizer and IonCube PHP Accelerator will implement this optimisation for you.
Absolute path
Echo
Dot
explode faster
http://net-beta.net/ubench/index.php?t=preg5
RAM > Database > File
Normally this is optimize MySQL query
Ex: We have 3 table: node, comment, user.
Get 10 latest node with comments + user information
Usually: 1 query to get 10 node, then for each node run 1 query to get list of comments and 1 query to get user information, then for each comment to get user information
=> 100~200 query :(
We can resolve this with 3 query.
Normally this is insert data to database or cache
Try to insert multiple rows in 1 time. Now MySQL, Memcached, MongoDB, Redis support it.
We shouldn't let user wait a process take a long time to finish: Sending email, edit file, insert to database, data mining
But PHP isn't a thread => how can we solve it?
Don't use Database to search if you can
By Sang Lê Thanh