0

I've been using a couple of reference variables with PHP 5.5 like this, from the result of a MySQL query:


    $sql="SELECT `name`, `value` FROM `maindatabase`.`systemProtect` WHERE `name`='IV' || `name`='passPhrase'";
    $result=mysqli_query($serverConn, $sql);
    while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
        $$row['name']=$row['value'];    
    }

Whilst I'm sure it might not be the best way to do things, it worked fine for my needs.

I upgraded to PHP 7.4 and I get a an error: Notice: Array to string conversion

Through a fair bit of trial and error, I've fixed this by using some additional {} The code is now

$sql="SELECT `name`, `value` FROM `maindatabase`.`systemProtect` WHERE `name`='IV' || `name`='passPhrase'";
$result=mysqli_query($serverConn, $sql);
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
    ${$row['name']}=$row['value'];  
}

Now everything is working again, but I'd really like to understand why - what is it that the {} are doing in this case? I'm assuming it's because the variable is part of an array... but this wasn't required before.

Thanks for any insight.

jack
  • 1
  • 2
  • 1
    There is probably a duplicate somewhere, but basically the first one could be interpreted as `$$row` and then apply the `['name']`, using the `{}` makes it clear where the association lies. – Nigel Ren Apr 20 '21 at 12:48
  • 2
    https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling – deceze Apr 20 '21 at 12:49
  • 1
    FYI: What you are talking about, is [variable variables](https://www.php.net/manual/en/language.variables.variable.php), not [references](https://www.php.net/manual/en/language.references.php). – CBroe Apr 20 '21 at 12:52

0 Answers0