Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP global keyword on an array value

Tags:

php

wordpress

Heyo

I'm currently working at a broken theme that someone else authored for WordPress. Upon a fresh install it throws a 500 error. After inspecting the code on my local machine, I'm able to lint the following:

    public static function skip_script($conf) {
    $hook_suffix = isset($GLOBALS['hook_suffix']) ? $GLOBALS['hook_suffix'] : null;

    if (isset($conf['variable'])) {
        global $$conf['variable']; 
    }

    $conditions = array(
        'variable' => isset($conf['variable']) && (!isset($$conf['variable']) || !$$conf['variable']),
        'hook_suffix' => isset($conf['hook_suffix']) && (is_null($hook_suffix) || $conf['hook_suffix'] != $hook_suffix)
    );

    return in_array(true, array_values($conditions), true);
}

The linter is upset with this:

if (isset($conf['variable'])) {
    global $$conf['variable']; # What the heck is this?
}

I can check the PHP error log and reproduce the same error on the server: PHP Parse error: syntax error, unexpected '[', expecting ',' or ';'

This WordPress theme is known to work in the past but appears to have been abandoned over the last 3 years.

Let me get to the heart of the question: What the heck is global $$conf['variable']; supposed to do. I'm assuming this is deprecated code, because it had worked before.

like image 543
trevdev Avatar asked Dec 19 '18 23:12

trevdev


People also ask

How do you make an array global?

There are two ways to reference a global variable in PHP: Use the global keyword at the start of every function that uses the variable. Use the $GLOBALS array.

What is global array in PHP?

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

Can we declare array as global variable?

The arrays can be declared and initialized globally as well as locally(i.e., in the particular scope of the program) in the program.

What does global keyword do in PHP?

The global keyword imports variables from the global scope into the local scope of a function.


1 Answers

That looks like a dynamic variable. I think it's trying to global the variable with the name of $conf['variable'].

You can fix the error like this, you just have to surround it with curly braces:

global ${$conf['variable']};

This has indeed been updated/changed in the newer version of PHP 7, as seen here:

http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.indirect

Changes to the handling of indirect variables, properties, and methods

Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. The table below shows how the order of evaluation has changed.

            Old and new evaluation of indirect expressions
| Expression          | PHP 5 interpretation  | PHP 7 interpretation  |
|---------------------|-----------------------|-----------------------|
| $$foo['bar']['baz'] | ${$foo['bar']['baz']} | ($$foo)['bar']['baz'] |
| $foo->$bar['baz']   | $foo->{$bar['baz']}   | ($foo->$bar)['baz']   |
| $foo->$bar['baz']() | $foo->{$bar['baz']}() | ($foo->$bar)['baz']() |
| Foo::$bar['baz']()  | Foo::{$bar['baz']}()  | (Foo::$bar)['baz']()  |

Code that used the old right-to-left evaluation order must be rewritten to explicitly use that evaluation order with curly braces (see the above middle column). This will make the code both forwards compatible with PHP 7.x and backwards compatible with PHP 5.x.

like image 122
Ethan Avatar answered Sep 23 '22 22:09

Ethan