这是我想要做的,我想构建一个程序,提示用户输入字符串,将字符串存储在数组中并提示用户输入另一个字符串,将该字符串存储在数组中并重复该过程5时间,但每次我运行程序,字符串被删除,它存储相同的字符串5次,我怎么能修改这个PHP代码,使它做我所说的?我尝试使用readline()但它不起作用,它说它是一个未定义的函数。
<form action = "index.php" method = "GET">
Name: <input type = "text" name = "tell" >
<br><br>
<input type = "submit" value = "submit">
</form>
<?php
$tester = array();
$counter = 5;
while ($counter > 0)
{
echo "please enter a criteria";
$name = $_GET['tell'];
array_push($tester,$name);
$counter = $counter - 1;
}
print_r($tester);
?>
答案 0 :(得分:0)
PHP仅在页面加载时运行,因此您的代码只从$ _GET获取一个值。为此,您必须启动会话并将数组存储在会话变量中。
<?PHP
session_start();
if($_GET['tell']) {
$tester = $_SESSION['tester'];
array_push($tester,$_GET['tell']);
$_SESSION['tester'] = $tester;
}
if(count($tester) < 5) {
//display your form to ask the user for more information
}