我需要一个简单的计数器在php中我知道,问题出在哪里,每当我启动该功能时,$counter
将再次跳到0.我不知道如何解决这个问题。 / p>
if(empty($_POST["message-to-send"])){
doSomething();
}
function doSomething(){
$counter=0;
$messages = array(
"message1",
"message2",
"message3",
"message4",
"message5"
);
echo $messages[$counter];
$counter++;
if($counter>count($messages)){
echo "something";
}
}
答案 0 :(得分:2)
如果你使$counter
成为静态变量,它将通过多次调用保持其值
function doSomething(){
static $counter=0;
$messages = array(
"message1",
"message2",
"message3",
"message4",
"message5"
);
echo $messages[$counter];
$counter++;
if($counter>count($messages)){
echo "something";
}
}
doSomething();
doSomething();
结果:
message1
message2