0

我正在尝试将未知数量的参数添加到 PHP 中的字符串。

这是代码:

<?php 
print "Get parameters";
$myFile = "wr.txt";
$fh = fopen ($myFile, 'w') or die ("can't open file");
$stringData = '';
//$st = '';
foreach ($_REQUEST as $k => $v) {
        $date = date ('Y-m-d H:i:s');
        $stringData = $k.":".$v."Date:".$date."\n";      
        fwrite ($fh, $stringData);
        fwrite ($fh, $stringData);
        fclose ($fh);
}
?>

但它只添加最后一个参数值。

我也试过喜欢

$stringData += $k.":".$v."Date:".$date."\n"; 

并把这个:

fwrite($fh, $stringData);
fwrite($fh, $stringData);
fclose($fh);

在 for 循环之外,但在这种情况下,它在wr.txt. 请帮助我如何按日期将所有参数写入一行。

谢谢

4

5 回答 5

2

InPHP .用于连接+,不用于连接。

改变这个

$stringData +=$k.":".$v."Date:".$date."\n"; 

$stringData .= $k.":".$v."Date:".$date."\n"; 
于 2013-02-22T06:08:53.603 回答
0

查看线路:

$stringData .=$k.":".$v."Date:".$date."\n";//** ADD CONCATENATION '.' BEFORE '='** 
fwrite($fh, $stringData); //PUT THIS OUT SIDE THE LOOP
fwrite($fh, $stringData); //**REMOVE THIS **
fclose($fh); // **PUT HIS OUT SIDE THE FOREACH LOOP** 

尝试这个 :

<?php 

print "Get parameters";
$myFile = "wr.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = '';
//$st = '';
foreach($_REQUEST as $k => $v) {
        $date = date('Y-m-d H:i:s');
        $stringData .=$k.":".$v."Date:".$date."\n";//** ADD CONCATENATION '.' BEFORE '='**  
        //fwrite($fh, $stringData); PUT THIS OUT SIDE THE LOOP
        ///fwrite($fh, $stringData); //**REMOVE THIS **
        //fclose($fh); // **PUT HIS OUT SIDE THE FOREACH LOOP**
}
fwrite($fh, $stringData);
fclose($fh);
?>
于 2013-02-22T06:16:35.843 回答
0

我只能猜测您要做什么。但试试这个:

<?php 
print "Get parameters";
$myFile = "wr.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = '';
//$st = '';
foreach($_REQUEST as $k => $v) {
    $date = date('Y-m-d H:i:s');
    $stringData = $stringData . $k.":".$v."Date:".$date."\n";
}
fwrite($fh, $stringData);
fclose($fh);
?>

希望有帮助。

于 2013-02-22T06:18:02.127 回答
0

利用 ”。” 而不是“+”,在 php 连接运算符是“。”

于 2013-02-22T06:14:48.330 回答
0

远离close ($fh)循环:

$myFile = "wr.txt";
$fh = fopen ($myFile, 'w') or die ("Can't open file");

$date = date ('Y-m-d H:i:s');
foreach ($_REQUEST as $k => $v)
    fwrite ($fh, "$k: $v Date $date\n") or die ("Cannot write to file");

fclose ($fh);
于 2013-02-22T06:11:13.900 回答