如何连接PHP变量名? [英] How to concatenate PHP variable name?

查看:31
本文介绍了如何连接PHP变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP for 循环:

I have a PHP for loop:

for ($counter=0,$counter<=67,$counter++){

echo $counter;
$check="some value";

}

我想要实现的是使用 for 循环变量并将其附加到另一个变量的名称.

What I am trying to achieve is use the for loop variable and append it to the name of another variable.

基本上,我希望每一行的 PHP 输出如下

Bascially, I want the PHP output to be as follows for each row

1
$check1="some value"

2
$check2="some value"

3
$check3="some value"

4
$check4="some value"

etc etc 

我尝试过 $check.$counter="some value" 但失败了.

I have tried $check.$counter="some value" but this fails.

我怎样才能做到这一点?我是否遗漏了一些明显的东西?

How can I achieve this? Am I missing something obvious?

推荐答案

变量变量 是:

${"check" . $counter} = "some value";

但是,我强烈反对这样做.使用 arrays数组.示例用法:

However, I highly discourage this. What you're trying to accomplish can most likely be solved more elegantly by using arrays. Example usage:

// Setting values
$check = array();
for ($counter = 0; $counter <= 67; $counter++){
    echo $counter;
    $check[] = "some value";
}

// Iterating through the values
foreach($check as $value) {
    echo $value;
}

这篇关于如何连接PHP变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆