dragonpeng200811111 2013-05-17 11:24
浏览 25
已采纳

如何在变量内插入变量

I Have the php code :

$rand = rand(1,5);

and I want to define var that has the name of the rand function like :

$$rand // if $rand= 1 then the var will be $1

and then do

switch($rand){
case(1):
$$rand = 'How many legs dog has ?';
$ans= '4';      }

The code is for defining security questions. Hope someone got my idea. How can I do it ?

  • 写回答

3条回答 默认 最新

  • dongpei2835 2013-05-17 11:31
    关注
    // Sanitize the arrays
    $questions = array();
    $answers = array();
    
    // Build some questions and assign to the questions array
    $questions[0] = 'How many legs does a dog have?';
    $questions[1] = 'How many eyes does a human have?';
    $questions[2] = 'How many legs does a spider have?';
    
    
    // Add the answers, making sure the array index is the same as the questions array
    $answers[0] = 4;
    $answers[1] = 2;
    $answers[2] = 8;
    
    
    // Select a question to use
    $questionId = rand(0, count($questions));
    
    
    // Output the question and answer
    echo 'questions: ' . $questions[$questionId];
    echo 'answer: ' . $answers[$questionId];
    

    Variables in PHP cannot start with a number.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误