ArticleDirectory
I 've been coding in PHP ever since I was 13, over the years I have seen
Includifferent coding styles and standards being used. However most
Them weren't optimised.
I have been writing PHP since I was 13 years old.CodeI have seen a lot of code with different styles and standards over the years. However, most of the time they have not been optimized.
This time I 'd like to talk about different ways on how you can speed up
Your scripts by optimizing your code to reduce your server load.
This time I want to talk about how to optimize your code to reduce the server load.
Coding tips
Encoding points
Quotes
Quotation marks
Try
Using single quotes as much as possible, it's faster than double quotes
Because PHP searches for variables in text surrounded in double quotes.
Use single quotes as much as possible, which is faster than double quotes, because PHP searches for variables in strings surrounded by double quotes.
Using single quotes in arrays is also recommended since it's faster than calling it with double or no quotes.
Single quotation marks are also recommended in arrays because they are faster than double quotation marks or no quotation marks.
Echo vs. Print
Echo and print
Echo
Is faster than print, if you're using concatenation in your echo
Command then you cocould optimise it further by using multiple parameters
Instead of concatenation.
ECHO is faster than print. Use single quotes as much as possible. If you want to use a connection in the ECHO command, optimize it to connect multiple parameters.
The print function can't handle multiple parameters so don't even try.
The print function cannot process multiple parameters. Therefore, do not try.
$ Name = 'Zenk0' ;
Echo 'The user' , $ Name , 'Has been selected for a special event .' ;
// Slower and more widely used
Echo 'The user' . $ Name . 'Has been selected for a special event .' ;
For Loops
Loop
Define your count variable before you start looping instead of in your loop. If you don't do this then your count function will be repeated everytime a loop happens.
Instead of in the circulatory body, the Count variable is defined in vitro. Otherwise, your counting function is called every cycle.
$ Array = Array ( 'One' , 'Two' , 'Three' ) ;
$ Count = Count ( $ Array ) ;
// Slow: For ($ I = 0; $ I <count ($ array); $ I ++)
For ( $ I = 0 ; $ I < $ Count ; $ I ++ )
{
Echo Array [ $ I ] ;
}
Simple conditionals
Simple Condition Judgment
It's better to use a switch statement instead of IF/else if/else when you're using simple conditionals.
If your condition is relatively simple, it is much better to use switch than to use if/else.
Includes & requires
Includes and requires
There are several ways to gain some speed in this are; first of all drop the use of require_once it's much slower than include_once. try to use full paths in your between des and requires, it'll spare the server some time on resolving the paths.
There are several ways to accelerate: first, using require_once is much slower than using include_once. Try to use full paths in your inclusion and reference, which saves the overhead of the parsing path.
There are 3 ways of doing this;
You coshould just simply set the include path.
There are three methods available;
You just need to set the include path.
// Works as of PHP 4.3.0
Set_include_path ( '/Inc' ) ;
// Works in all PHP versions
Ini_set ( 'Include _ Path' , '/Inc' ) ;
Or you cocould create a variable with the include path and concatenate it at the start of an include.
Or create a new path variable for inclusion and put it into the include link.
$ Include = '/Home/user/WWW' ;
Include ( $ Include . '/Library/loader. php' ) ;
As a third example; You cocould also just get the directory path from the current file.
Example 3: You can only obtain the path of the current file.
$ Dir = Dirname ( _ File __ ) ;
Include ( $ Dir . '/Library/loader. php' ) ;
Handling strings
Processing string
Try to make use of as much STR _ functions as possible instead of turning to preg _ functions.
Use the STR _ function instead of the preg _ function.
Str_replace is much faster than preg_replace if you're not making use of RegEx patterns. In turn strstr is faster than using str_replace.
If you do not need regular expressions, str_replace is much faster than preg_replace. However, strstr is faster than str_replace.
Try to make use of these functions instead of preg _ Functions: strpos, strpbrk, Strncasecmp.
Use these functions as much as possible to replace the preg _ Functions: strpos, strpbrk, and strncasecmp.
If you need to check if a string has a certain length it's better to use the isset trick than using strlen.
If you need to check the length of a string, the clever use of isset is much better than strlen.
$ Str = 'Have a nice day' ;
// Check if the string has more than 6 Characters
// Slow, checks if the string has less than 7 characters.
If ( Strlen ( $ Str ) < 7 )
// Fast, if there is no seventh Character Set
If ( ! Isset ( $ Str { 6 } ) )
This is because isset is a language construct, whereas strlen is a function that is looked up.
This is because isset is a language structure and strlen is a function.
Ofcourse there are several tools than can help you; use a code profiler.
Of course, there are some tools to help: Use A code profiler.
They will tell you which how much time is spent parsing your script and which part requires the most time. This makes it easy to find bottlenecks in your code.
It will tell you the time-consuming analysis of script running and the time used most. This allows you to easily find the bottleneck in your code.