Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

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

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];
      $cu[$k]=$$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?
PHP

Avatar of undefined
Last Comment
David Favor

8/22/2022 - Mon
David Favor

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 Favor

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.
gr8gonzo

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Dave Baldwin

At first I thought that 'global' was an error but apparently not.
http://php.net/manual/en/language.variables.scope.php

Apparently $$ is used to create variable variable names.
http://php.net/manual/en/language.variables.variable.php
ASKER CERTIFIED SOLUTION
gr8gonzo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Bruce Gust

ASKER
Gentlemen!

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 Favor

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.