rss

PHP Code Optimization Program

Inevitably, write code effectively and efficiently is the key to an important basis for improving the speed of code execution. As is known, the PHP code will always be executed every time he was requested by the client (browser). Therefore, a very important advantage if
You can create an effective and efficient code.

Here are some tips and strategies to optimize your PHP application program code in general.
• Avoid the space bar to set the code
Avoiding the use of space (or) excessive step is efficient.
Consider, every space is 1 byte and each tab (\ t) is also 1 byte. When you make
four spaces, you have spent 4 bytes of data. Would be more efficient if only
using a tab.
• How to use the data type boolean
PHP allows you to write a boolean data type with a small or large letters
(Case-insensitive). Nevertheless, writing in small letters will all be much faster
compared to uppercase. This is because, when he found a constant, PHP performs a lookup hash
name constants.
if ($ var = TRUE) {
   .....
}
/ / To be executed more quickly
if ($ var = true) {
   .....
}

In addition, when working with boolean values, using the values 1 and 0 more quickly
than the true and false.
• Avoid large string concatenation
At the time of doing string concatenation, avoid merging with a string of size
great. This could hamper the actual executable code can be displayed quickly.
Examples include the following:

/ / Concatenation of strings
$ 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;


• Print output
Three common ways to print data to the output is: direct output, echo (), and
print (). When the program code does not contain PHP code, printing with direct output
more effective and efficient. Direct Example output like the following:
<? Php
/ / Php code
?>
Direct output
<? Php
/ / Php code
?>


If the condition you need to print the output using the function
(Language constructs) PHP, use the echo (), rather than print (). Although an outline of the print ()
and echo () has the same purpose, but there are some essential differences that need to be
be noted.
Function print () behaves like a function in general, and has a return value (return
value) is an integer 1. Thus, the print () can be used as part of the expression
more complex. Meanwhile, the echo () is capable of receiving more than one parameter at a time,
and has no return value.

print 'String 1';
echo 'String 1';
/ / Using multiple parameters
echo 'String 1', "String 2",'...';


String functions echo () will be executed more quickly than the print (). This difference is
due to the function print () will return the status (integer) that says if
process successfully implemented or not.
On the other hand, echo () only displays output only and does not do anything else. There was
in its implementation, the status of the return value from the use of string functions hardly
ever needed.
• Check the length of the string
The common way to check the length of characters is to use the function strlen ().
For the same purpose, there is actually a faster way, using isset ().
Examples include the following:
if (strlen ($ str) <5) {
   echo "String must be at least 5 chars';
}
if (isset ($ str {5})) {
   echo "String must be at least 5 chars';
}


As with the case of echo () and print (), isset () requires a shorter execution time because
it is a language construct.
Language construct echo () also allows you to give more than one string
as a parameter. Using some of the parameters will be faster than the mixing
variables into a single parameter. Examples include the following:

$ A = 'Hello';
$ B = 'World';
echo 'Say'. $ a. 'To'. $ B;
/ / Faster
echo 'Say', $ a, 'to', $ b;

No comments:

Post a Comment

 

Popular Posts

Followers