我想将读取的文件输出存储在变量中

i want to store readed file output in variable

in PHP 我希望 "fgets()" 函数在单独的变量中读取每个字符串。 下面是我的代码

$handle = @fopen("txtfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

这段代码的输出是:

I am Demo

我希望此输出为:

$word1 = I
$word2 = am
$word3 = Demo
<?php

$handle = @fopen("txtfile.txt", "r");
$text = '';
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        $text .= $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

$array = explode(' ', $text);

$i = 1;

foreach ($array as $word) {
    ${"word$i"} = $word;
    $i++;
}

现在每个单词都保存在不同的变量中