为什么strlen计算的长度比实际长度高出两个


Why is strlen calculating the length is two higher than the length actually is?

我有以下代码:

<?php
if (!isset($_SESSION["word"])) {
    $dictionary_file = new SplFileObject("dictionary.txt");
    $dictionary_file->seek(rand(0, 80367));
    $_SESSION["word"] = $dictionary_file->current();
    $_SESSION["word_progress"] = "";
    for ($i = 0; $i < strlen($_SESSION["word"]); $i++) {
        $_SESSION["word_progress"] .= "_";
    }
    echo strlen($_SESSION["word"]);
    echo $_SESSION["word"];
}
else {
    if (isset($_GET["guess"])) {
        // If their guessed letter is in the word
        if (stripos($_SESSION["word"], $_GET["guess"]) !== false) {
            $occurrence_points = strposall($_SESSION["word"], $_GET["guess"]);
            $progress = $_SESSION["word_progress"];
            foreach ($occurrence_points as $values) {
                $progress[$values] = $_GET["guess"];
            }
            $_SESSION["word_progress"] = $progress;
        }
    }
}
?>

我从文件中随机生成一个单词。说它会产生"泡沫"。长度是6,但它会报告8,即使在打印出来并确认这个词确实是泡沫之后。为什么会这样?

随机生成的单词可能会添加空白。做修剪()

   $_SESSION["word"] = trim($dictionary_file->current());