Thousand of active PHP variables

I use PHP simple variables for website multi-language options to keep the MySQL database requests minimal, not using for things that really don’t need it. One language, one PHP file. So one file contains thousand(user terms, menus, email content and everything else) or more variables that are always accessible, all the time.

Example:

// language_en_us.php
$lang[1]="Welcome to my site";
$lang[10000]="Terms of service and some HTML";

I just want to know if this is okay approach, looking at the performance point. Is there such thing as setting too many variables… or any other issues?

Thank You

Why?

Database tables have been on the go for a very long time and I would have thought ideal for storing language information.

By not having to load the rather large PHP file the application should be a lot quicker to render. Indexed table searches should be very fast.

Updating a table record should also be very quick and faster than having to edit and save a large PHP file.

The idea is fine, but I’d give them some more meaningful names, like

$lang['welcome']="Welcome to my site";
$lang['TOS']="Terms of service and some HTML";

You may also want to look at a real translator system, like the Symfony Translation component

it’s much faster than MySQL engine.

Not really, but if you want to handle multilingual content, there are better solutions.

Take a look at getText, it is one of the most used solutions to handle multilingual content.
https://www.gnu.org/software/gettext/

PHP has implemented support directly into the language as well:
https://www.php.net/gettext

An easy and free software to manage the translations is:

Now, getText is powerful, allowing you to handle how a language deals with plurals, etc. it is quick, easy to use, and most important very easy to translate the content to new languages.

While in most cases, some language content will always need to be stored in a database for a multilingual website, it is a lot harder to manage than using getText.

If you store language content in the database, the queries are more complicated as you need fallbacks as well, what if the user requests Spanish, but the phrase in question is only in English.

It is also a lot harder to pass along the content to a translator, and to update the content afterward. Internally we have handled this by an own system that takes all content from the different tables, and create getText content files with it that can be passed along to a translator, and then we just upload the getText file we get in return, and it will automatically create the new phrase records, or update the old if the language already exists. Note, as mentioned above, we only store content in the database that is expected to be easily edited by the client, like emails, product names etc.

It is basically just a wrapper, in my opinion, it is a lot easier just using getText directly.

It’s the opposite. File access is a lot faster than network access. Plus for PHP files there is opcache, so the file only has to read once and then it’s in memory.

1 Like

Sure, if you have root access to whatever is running PHP (Apache, PHP-FPM, etc) and can restart it every time you deploy then yes, gettext is great. If you don’t then gettext is very very annoying.

Also, Symfony Translation has multiple possible sources, it can indeed be used with gettext, but you can also use JSON, YAML, PHP array, databases, etc. It doesn’t really care where you store things.

In the event you do not have access to restart the server when you deploy, the chance that you will run any type of multilingual application is slim.

Though, you can utilize getText with your own implementation if you want, which can be setup so you wont require a webserver restart to replace the content file.

On a side note though, if you run php-fpm with opcache, to obtain a issue free deployment, you need to restart the php-fpm to clear opcache.

I am using the PHP file approach because it seems to be super easy, simple and reliable. Since language detection system is already developed, making a new file, adding another language is super easy. I don’t know what does the translator personal do with the file if I send him that.

Of course I use MYSQL translation tables on things that are more complicated and can’t be solved with simple files or it is not reasonable (inside DB content), with import and export system.

Referring to machine translations? Machines can’t translate content, we are not that far yet…there is no room for error.

Thank you all for the informative posts