Answer the question
In order to leave comments, you need to log in
How to optimize a piece of PHP code?
I wrote a self-written site, now I started to optimize some code in order to speed up the site.
There is this code:
$language = explode("/", $_SERVER['REQUEST_URI']);
$language = $language[1];
if($language == "ru") {
$lang = "2";
require_once(SYSTEM_DIR . "languages/lang_russian/lang_main.php");
} else {
require_once(SYSTEM_DIR . "languages/lang_ukrainian/lang_main.php");
$lang = "1";
}
Answer the question
In order to leave comments, you need to log in
Are you kidding me?
Leave this code alone, I also found mega-optimizers.
Is it possible to put this part of the code on memache? - You can, but you don't have to.
Store the language selected by the user in cookies (not the language itself, of course, but its label), if without authorization. If with authorization it is possible and to write down in a DB. Watch what you want.
But in any case, you will have to do some kind of check or other manipulation to connect a specific language. But don't worry, it won't affect performance. Do this check at least 100 times, you will not notice the difference.
It is necessary to optimize the settings of the server, PHP, database, cache the results of long calculations and slow queries.
In the code, pay attention to sections with iteration of arrays, nested arrays. This is where the performance issue is most likely to occur.
If you are not doing anything criminal in the code, database queries are fast, there are no calls to external resources, but the site speed is low, then your server is most likely slowing down due to incorrect.
If you constantly have to check and connect the file, do this:
<?php
$language = explode("/", $_SERVER['REQUEST_URI']);
$language = $language[1];
if($language == "ru") {
$_SESSION['lang'] = "2";
$_SESSION['lang_file'] = "lang_russian";
} else {
$_SESSION['lang'] = "2";
$_SESSION['lang_file'] = "lang_ukrainian";
}
require_once(SYSTEM_DIR . "languages/".$_SESSION['lang_file']."/lang_main.php");
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question