如何将变量放入数组

时间:2018-12-01 21:20:27

标签: php

我怎么

$example = 'banana';
$answer = array('i love eating $exmaple','$example is my favorite food','everywhere eating $banana');

echo $answer[rand(0,2)];

2 个答案:

答案 0 :(得分:1)

使用双引号引起来的字符串。

$example = 'banana';

$answer = array("i love eating $example","$example is my fovorite food","everywhere eating $example");

echo $answer[rand(0, 2)];

答案 1 :(得分:1)

<?php    
$example = 'banana';
$answer = array('i love eating ' . $example, $example . ' is my fovorite food', 'everywhere eating ' . $banana);
echo $answer[rand(0,2)];


$example = 'banana';
$answer = array("i love eating $example", "$example is my fovorite food", "everywhere eating $banana");
echo $answer[rand(0,2)];


$example = 'banana';
$answer = array("i love eating {$example}", "{$example} is my fovorite food", "everywhere eating {$banana}");
echo $answer[rand(0,2)];

有几种方法可以做到。上面所有3个示例都做同样的事情。

编辑:您必须原谅我的双引号-我目前正在使用iPhone。