2

如何将 IF THEN 表达式放入 PHP 函数中fwrite?下面给出的代码不能正常工作。请看线fwrite($fh, "$row[0]...)

    $myFile = "testFile.php";
    $fh = fopen($myFile, 'w') or die("can't open file");

    fwrite($fh, "lat    lon title   description iconOffset  icon\n");
    foreach ($result4 as $row):
        fwrite($fh, "$row[0]    $row[1] $row[2] Resource average speed is: $row[3] km/h -10,-10 if($row[4]==0) images/markerRed.png else images/markerGreen.png\n");
    endforeach;

    fclose($fh);
?>
4

3 回答 3

4
$image = ($row[4] == 0) ? "images/markerRed.png" : "images/markerGreen.png";
/* ... $row[3] ... ".$image."\n"; */
于 2012-05-22T13:35:25.790 回答
1

试试这个:

foreach ($result4 as $row) 
{
    $s = "$row[0]    $row[1] $row[2] Resource average speed is: $row[3] km/h -10,-10 ";
    $s .= $row[4]==0 ? 'images/markerRed.png' : 'images/markerGreen.png';
    fwrite($fh, $s."\n");
}
于 2012-05-22T13:35:32.730 回答
1

fwrite不会理解或评估 PHP 逻辑。像这样分开if语句:

fwrite($fh, "$row[0]    $row[1] $row[2] Resource average speed is: $row[3] km/h -10,-10");
if($row[4]==0)
    fwrite($fh, " images/markerRed.png\n");
else
    fwrite($fh, " images/markerGreen.png\n");
于 2012-05-22T13:36:24.513 回答