H
H
hdtor2016-02-16 11:01:51
PHP
hdtor, 2016-02-16 11:01:51

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";
}

He learns what language to give to the reader. How can it be optimized?
Is it possible to put this part of the code on memache?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
IceJOKER, 2016-02-16
@IceJOKER

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.

S
Silm, 2016-02-16
@Silm

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.

A
Alexey, 2016-02-16
@dzheka3d

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");
?>

And then pull out these sessions and business ...

A
Anton, 2016-02-16
@SPAHI4

99% of performance problems are database-related operations, and then everything is ok in terms of performance, but logic is another matter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question