PHP Tips And Tricks
File Access Optimization
When you need to including file, please recheck file content that will be use. If file is
not contains PHP codes, use readfile() function to increase performance.
Because files that including with readfile() does not parsing by PHP. It will be different
with construction language include() and require(), and files will be evaluated before.
Consequence from using readfile() is increase attack risk. This risk will be happen when
accessing file from URL.
Solution for the attack risk is by using File Handling.
Using echo
Echo allowed gives more than one string as parameter. Using some parameters are going to
more faster than blending some variables into a parameter.
$a = ‘Hello’;
$b = ‘Word’;
echo ‘Say ‘ .$a. ‘ to ‘ .$b’;
//more faster
echo ‘Say ‘ , $a, ‘ to ‘, $b’;
Checking string size
We usually using strlen() function to check the string size. The fast way to checking string
size is using isset().
if (strlen($str) <>
echo ‘String must be at least 5 chars’;
}
if (!isset($str{5})) {
echo ‘String must be at least 5 chars’;
}
isset() needs more little time than strlen() because isset() is construction language.
Avoid using Large String Concatenation
When do concatenation string, avoid uniting with large size string. It can obstructing code
execution that really can display more faster.
//large string concatenation
$title = ‘title’;
$body = ‘…a very large block…’;
echo “Subject: $title\n\n$body”;
//avoid large string concatenation
$title = ‘title’;
$body = ‘…a very large block…’;
echo “Subject: $title\n\n”;
echo $body;
Boolean data type
PHP is allowing to write Boolean data type with uppercase or lowercase. But, writing with
lowercase is more faster than uppercase. When found a constant, PHP do lookup hash constant
name.
if ($var = TRUE) {
...
}
//this is more faster
if ($var = true) {
...
}
When using a Boolean value, 1 and 0 are more faster than true and false.
Avoid space in your code
In this tip and trick, I’m going to explain how to make code optimization on your
application. The main purpose of this code optimization is to get more faster code
execution. The elementary key is by writing code effectively and efficiently. Because PHP
codes going to execute every time they are requested.
Avoid space in your code
Avoid using many spaces ( ) is a good thing. Every space is 1 byte and every tab (\t) is 1
byte. When you’re using four spaces, you’ve been use 4 bytes. It’s more effective if you’re
using a tab (\t).
Print Output
PHP is giving some sting functions to printing output into browser and we are often using
print() and echo() function. Both of those functions are not real function but a language
construction. print() and echo() function have same goal, but there are some essential
different that must to pay attention.
Print() function behavior like the other function in common and having return value integer
1. Thus, print() can used as part of expression which more complex. Meanwhile, echo() is
able to accept more than one parameters all at once, and does not having return value.
print() ‘string 1’;
echo ‘string 1’;
// using some parameters
echo ‘string 1’, “string 2”, ‘…’;
echo() function string will execution more faster than print(). This differentiate caused by
will return status (integer) which expose what process has done or not.
On the other side, echo() just displaying output only and do anything. At implementation,
return value status from using function string almost never needed.
File Access Optimization
When you need to including file, please recheck file content that will be use. If file is
not contains PHP codes, use readfile() function to increase performance.
Because files that including with readfile() does not parsing by PHP. It will be different
with construction language include() and require(), and files will be evaluated before.
Consequence from using readfile() is increase attack risk. This risk will be happen when
accessing file from URL.
Solution for the attack risk is by using File Handling.
Using echo
Echo allowed gives more than one string as parameter. Using some parameters are going to
more faster than blending some variables into a parameter.
$a = ‘Hello’;
$b = ‘Word’;
echo ‘Say ‘ .$a. ‘ to ‘ .$b’;
//more faster
echo ‘Say ‘ , $a, ‘ to ‘, $b’;
Checking string size
We usually using strlen() function to check the string size. The fast way to checking string
size is using isset().
if (strlen($str) <>
echo ‘String must be at least 5 chars’;
}
if (!isset($str{5})) {
echo ‘String must be at least 5 chars’;
}
isset() needs more little time than strlen() because isset() is construction language.
Avoid using Large String Concatenation
When do concatenation string, avoid uniting with large size string. It can obstructing code
execution that really can display more faster.
//large string concatenation
$title = ‘title’;
$body = ‘…a very large block…’;
echo “Subject: $title\n\n$body”;
//avoid large string concatenation
$title = ‘title’;
$body = ‘…a very large block…’;
echo “Subject: $title\n\n”;
echo $body;
Boolean data type
PHP is allowing to write Boolean data type with uppercase or lowercase. But, writing with
lowercase is more faster than uppercase. When found a constant, PHP do lookup hash constant
name.
if ($var = TRUE) {
...
}
//this is more faster
if ($var = true) {
...
}
When using a Boolean value, 1 and 0 are more faster than true and false.
Avoid space in your code
In this tip and trick, I’m going to explain how to make code optimization on your
application. The main purpose of this code optimization is to get more faster code
execution. The elementary key is by writing code effectively and efficiently. Because PHP
codes going to execute every time they are requested.
Avoid space in your code
Avoid using many spaces ( ) is a good thing. Every space is 1 byte and every tab (\t) is 1
byte. When you’re using four spaces, you’ve been use 4 bytes. It’s more effective if you’re
using a tab (\t).
Print Output
PHP is giving some sting functions to printing output into browser and we are often using
print() and echo() function. Both of those functions are not real function but a language
construction. print() and echo() function have same goal, but there are some essential
different that must to pay attention.
Print() function behavior like the other function in common and having return value integer
1. Thus, print() can used as part of expression which more complex. Meanwhile, echo() is
able to accept more than one parameters all at once, and does not having return value.
print() ‘string 1’;
echo ‘string 1’;
// using some parameters
echo ‘string 1’, “string 2”, ‘…’;
echo() function string will execution more faster than print(). This differentiate caused by
will return status (integer) which expose what process has done or not.
On the other side, echo() just displaying output only and do anything. At implementation,
return value status from using function string almost never needed.