English 中文(简体)
PHP 函数插入错误位置的值
原标题:PHP function inserts values in wrong place
  • 时间:2012-05-24 14:48:18
  •  标签:
  • php

下面的代码在目录中获取所有.csv 文件的列表。 我创建了函数 < 坚固> csvcolumum , 从每个文件中提取一些信息( 第 3 栏中所有值的总和), 并在每个列表项中输入 。

html 输出应该看起来是这样的 :

<li>Filename (sum)</li>

但结果是这样的:

sum<li>Filename ()</li>

有人能指出为什么会发生这种情况吗?这是相关的代码。

require("csvcolumnsum.php");

echo "<ul class="users">";
$handle=opendir("data");
while (($file = readdir($handle))!==false) {
    $file = preg_replace("/\.[^.\s]{3}$/","",$file);
    if (preg_match( /[^.]/i , $file)) {
        $value = csvcolumnsum("data/".$file.".csv",3);
        echo "<li><a href="?form_name=$file">$file(";
        echo $value;
        echo ")</a></li>";
    }
}
closedir($handle);
echo "</ul>";

< 加强> csvcluclumumum.php

function csvcolumnsum($filename,$col) {
    $handle2 = fopen($filename,  r );
    $data = fgetcsv($handle2);

    foreach ($data as $headercolumn) {}
    while ($data = fgetcsv($handle2)) {
        $sum += $data[$col];
    }

    echo "<span>$sum</span>";

    fclose($handle2);
}
最佳回答

这不是 echo 是如何运作的。 如果您想要您函数将一个值回传到调用函数, 您的函数需要将该值( code> return ) 转换为 < code> , 而不是 < code> > /code> :

return "<span>$sum</span>";

Echo直接写给标准输出,

$value = csvcolumnsum("data/".$file.".csv",3);

在这些行之前的指纹 :

echo "<li><a href="?form_name=$file">$file(";
echo $value;
echo ")</a></li>";

返回当前函数中的停止执行, 意思是您的 close 将需要在 return 之前发生:

function csvcolumnsum($filename,$col) {
    $handle2 = fopen($filename,  r );

    # ...

    fclose($handle2);

    return "<span>$sum</span>";
}
问题回答

csvcolumumum 函数中,您重置 >> return 功能中的 return

你呼应你必须归还的金额,

function csvcolumnsum($filename,$col) {
    $handle2 = fopen($filename,  r );
    $data = fgetcsv($handle2);

    foreach ($data as $headercolumn) {}
    while ($data = fgetcsv($handle2)) {
        $sum += $data[$col];
    }

    return "<span>$sum</span>";

    fclose($handle2);
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签