Odd issue to with Defining Constants

Hello I am testing things and i recently uploaded this code on a different hosting but for some reason it is not translating properly.

Here is the code:

/*
* --------------------------------------------------------------------------
* Static Language System
* --------------------------------------------------------------------------
* Loads correct language array depending on LANGUAGE setting.
*/

if(LANGUAGE === 'custom') {
    $config['lang_custom'] = include_once(LANG_PATH . 'custom.php');
    define('LANG', $config['lang_custom']);
} else {
    $config['lang_en'] = include_once( LANG_PATH . 'en.php');
    define('LANG', $config['lang_en']);
}

Language file:

<?php

return [
    'this' => 'this',
    'this1' => 'this1',
    'this2' => 'this2',
];

then on every page you can then go

<?php echo LANG['this']; ?> and it will echo ‘this’; but for some reason on this host it just shows an L, not errors nothing.

Can anyone see where/why this is happening? (again works on every other server i have tested)

Thanks

Try echoing $config['lang_custom'];

I think it will be a string and not an array. Check using gettype(…)

nope, it’s an array. It’s odd.


echo gettype($config['lang_en']);

The new host you’re trying it on is running PHP 5.x instead of 7.x.

PHP 5.x doesn’t allow to define arrays, it only allows defining scalars (strings, ints, floats, etc)

So what happens is first you call define('LANG', ['this' => 'this', etc]), but PHP 5 doesn’t allow that, so it emits a warning (which is not shown on your server because display_errors is set to 0, as it should be):

Warning: Constants may only evaluate to scalar values in /in/8Lpdi on line 3

This is a warning, so the script doesn’t stop, but LANG is not defined, because the value is invalid.

So then when you call LANG['this'], it will emit another warning, saying that LANG is not defined:

Notice: Use of undefined constant LANG - assumed 'LANG' in /in/8Lpdi on line 5

As you can see here PHP says LANG is not defined, so it will assume you meant the string "LANG".

Next it will try to get the index this of the "LANG" string, but strings don’t have string indices, so that gives a warning too:

Warning: Illegal string offset 'this' in /in/8Lpdi on line 5

What PHP in it’s infinite wisdom will now decide is that it can do numeric indices on strings, so it will convert this to an integer, which is 0. So the result is that PHP will echo the result of "LANG"[0], which is to say the first character of "LANG", which is indeed the L and that explains why you’re seeing L everywhere.

See https://3v4l.org/8Lpdi for an example of all the warnings etc

2 Likes

Thankyou very much, I will get the guy to upgrade his server.


Also can i ask what all you developers use for scanning code for vulnerabilities?

Thanks

That’s another topic. Please start a new thread with that question :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.