We help IT Professionals succeed at work.

What's wrong with this line of code in light of the server recently updating its version of PHP?

Bruce Gust
Bruce Gust asked
Last Modified: 2019-01-17
I'm working with a client who's experiencing a great deal of angst because of some functions on her page suddenly not working because of, what I believe to be, an update to the PHP version her sight is using.

I've already repaired a scenario where the site was using mysql as opposed to mysqli, but I'm looking at something now that I'm not familiar with and i'm hoping someone can give me some insight knowing that the server has recently been updated.

Here's the code that's throwing the error:

for ($k=1;$k<=50;$k++)
      global $$SitelokCustom[$k];

...and here's the error:

Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home1/nomasint/public_html/_vibralogix/vibralinklokipn/linklokipn.php on line 3318

I've never seen two $ signs before, but apparently that was working at one point. I started by eliminating that, but it didn't have any affect.

Any ideas?
Watch Question

David FavorFractional CTO
Distinguished Expert 2019

PHP is to complex to guess about compatibility problems.

Use https://github.com/PHPCompatibility/PHPCompatibility to know, rather than guess.

The phpcs command finds compatibility issues.

The phpcbf command auto fixes many compatibility issues.
David FavorFractional CTO
Distinguished Expert 2019

The double $$ does look odd + no way to guess with such a small fragment of code.

If all the code worked prior to an update + fails after an update phpcs will likely help.

And... could be you were running a very old PHP, so this code may have never worked correctly + newer versions of PHP simply raise an exception for code that never worked.

The double $ is technically fine. Basically, it is a variable variable. PHP evaluates the inner variable first and uses the result as the outer variable name. For example:

$apple_color = "red";
$grape_color = "green";

$dynamic_color = "apple_color";
echo $$dynamic_color; // "red"

$dynamic_color = "grape_color";
echo $$dynamic_color; // "green"

Usually $$ is a pretty bad idea. It opens up code to more security risks, especially if a user can manipulate the inner variable contents.

Chances are that in your case, there is some other problem and the output of that error or warning is getting into your inner variable ($SitelokCustom) and when it tries to evaluate the outer variable, it's not a valid variable.

You can echo the inner variable to see if the contents have something unexpected. Either way, I would suggest rewriting that bit of code to NOT use the $$ functionality.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

At first I thought that 'global' was an error but apparently not.

Apparently $$ is used to create variable variable names.
This one is on us!
(Get your first solution completely free - no credit card required)
Bruce GustPHP Developer



Thank you very much!

To answer your questions / concerns, this is antiquated code belonging to a client that has needed to put some things off, as far as updating her site, because of financial and logistical concerns. Recently, however, her site failed to function at some key points leaving customers hanging and she's now having to scramble.

Whether she's running PHP 5 or 4, I'm not certain. But it's old and, as noted, there are some questionable techniques being used that make a bad situation worse.

Gonzo, I used your bandaid and it worked. I'm downloading the rest of the code now to see where there are other "mysql" as opposed to my "mysqli" dynamics to see if we can't get this thing up and running. That's what I had been working with before, but "$$" was something new and I do appreciate the help!
David FavorFractional CTO
Distinguished Expert 2019

You mentioned, "Whether she's running PHP 5 or 4, I'm not certain."

This is why phpcs is so useful.

No guessing. You can target specific versions like... having phpcs tell you...

Show me every line of code, in all my .php + .lib + .inc + .config + .html, which will fail on PHP-7.2 or PHP-7.3 or above.

Using phpcs will save you a massive amount of time.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.