简体   繁体   English

PHP变量内部数组

[英]PHP Variable inside array

I have a variable: 我有一个变量:

$my_var = page

And want to put it into an array: 并希望将其放入一个数组:

 <?php 
    $opts = array(
        'page'=>'/subfolder/ <!-- $my_var ---> .php',
    );
    content_custom('content-area', $opts); 
?>

That I will get the path like: 我会得到这样的道路:

/subfolder/page.php

I'm not so familiar with php. 我对php不太熟悉。 Is this possible? 这可能吗?

$opts = array(
    'page'=>"/subfolder/{$my_var}.php"
);

That will work fine. 那会很好。 However, when you put a string in SINGLE quotes, variables inside it aren't evaluated. 但是,当您将字符串放在SINGLE引号中时,其中的变量不会被计算。 Use double quotes around the variable you're including in the string, and it will work. 在字符串中包含的变量周围使用双引号,它会起作用。

You can also use the . 你也可以使用。 operator to concatenate strings: 运算符连接字符串:

Neal's example: 尼尔的例子:

$opts = array(
    'page'=>'/subfolder/'.$my_var.'.php'
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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