Avatar of Bruce Gust
Bruce GustFlag 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
Avatar of David Favor
David Favor
Flag of United States of America image

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.
Avatar of David Favor
David Favor
Flag of United States of America image

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.
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

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!
Avatar of David Favor
David Favor
Flag of United States of America image

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

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo