简体   繁体   中英

Php variable increment

So i have the following code that works and outputs the information let's say 2 11 . I would like to have $keyidcall increment in variable name so $keyidcall2, $keyidcall3....... with the additional variables I would like the variable to hold the correct value so I can call it later. so expected output would be $Apiammount ="2" ; echo $keyidcall; would echo 2 echo $keyidcall : would echo 11

while($Apiammount > 1){
    $Keyidquery = mysqli_query($connection, "SELECT ID FROM `Characterapi` WHERE UserId = '$Idcall'");
    while($keyid = mysqli_fetch_assoc($Keyidquery)){
        $keyidcall = $keyid['ID'];
        echo $keyidcall;
    }

    $Apiammount--;
}

The better way to do this would be to store the values in an array.

$keyidcall[] = $keyid['ID'];

Then you can refer to them later as

echo $keyidcall[0]; echo $keyidcall[1];

in the order that they were entered in.

Or if you wanted something more specific to refer to it by, you could use

$keyidcall[$Apiammount] = $keyid['ID'];

then you would refer to them as:

echo $keyidcall[<apiamount>];

Assuming you know what that would be.

You can do:

$i = 0;
while($foo){
  $name = 'keyidcall';
  $i++;
  $newvar = $name . $i;
  echo $$newvar;
}

And $$newvar will echo the value of "keyidcallX" where X is an incrementing value. Not sure if that's what you meant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM