php我的while语句是关闭增量不起作用

时间:2015-10-07 23:58:57

标签: php html

这是我对分号文本文件数据库的输出

MSG2;This is the first test;William;
MSG2;This is the second test;William;
MSG2;This is the third test;William;
MSG1;This is the third test;William;

我正在拍摄的是这个...

MSG 1;This is the first test;William;
MSG 2;This is the second test;William;
MSG 3;This is the third test;William;
MSG 4;This is the third test;William;

这是我的变量

<?php
$x = 1; 

while($x < 1) {
    $x++;
} 
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = 'MSG'  .  $x . ';' .  $_POST['field1'] . ';' . $_POST['field2'] . ';'   ."\n";
$ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die('There was an error writing this file');
}
else {
    echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
 }           
 ?>

我试图理解num ++的概念。我该如何解决我的问题?谢谢。

1 个答案:

答案 0 :(得分:1)

看起来你的循环已关闭

$x = 1; 

while($x < 1) {
    $x++;
} 

所以,首先,你的while循环只做一件事。其余代码此循环之外。所以这个循环对你的其余代码毫无意义。

其次,您将$x设置为等于1,然后说明$x是否小于1,将{1}添加1,直到它等于1 ......它已经存在了。

你应该做的是这样的事情(伪代码,因为你没有任何东西可以循环)

$x