PHP在字符串文字中动态引用变量 [英] PHP dynamically reference variable in string literal

查看:107
本文介绍了PHP在字符串文字中动态引用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个PHP变量,形式为

I have multiple PHP variables in the form

$number1, $number2, $number3 and so on...

我想在循环中动态地引用这些内容以从中检索信息,但不知道如何动态引用静态变量。例如:$($ i = 1; $ i = 10; $ i ++){
//

I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:

for($i = 1; $i <= 10; $i++) {
    //The first number to be printed should be the value from
    //$number1 not $number concatenated to $i

    //here are some of the strings I tried:
    echo "$number$i";
    echo "{$number}$i";
    echo "{$number}{$i}";
}


推荐答案

应该这样做: / p>

This should do it:

echo ${"number{$i}"};

但是为什么不使用数组? $ number [$ i] 更易读...

But why not use arrays instead? Having $number[$i] is much more readable...

这篇关于PHP在字符串文字中动态引用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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