【发布时间】:2017-01-01 04:49:29
【问题描述】:
我有一个 PHP 脚本,它从文件 fileRead2.php 调用函数 fileRead2 函数。
下面的函数读取username.txt(显示的是用户名)。
vim fileRead2.php
<?php
function fileRead2() {
global $fh, $line;
$fh = fopen('username.txt','r');
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
echo($line);
}
fclose($fh);
}
?>
如果我在 linux 文件系统上运行 linux 命令 cat,它会显示 'tjones'(用户名)。
我在脚本中运行以下内容。
<?php
// Read the Username
require_once('fileread2.php');
$userName = fileRead2();
echo $userName;
var_dump($userName);
>?
它回显了显示的“tjones”的$userName,但是 var_dump 显示其输出为 NULL。
var_dump 是否有任何理由将$userName 变量显示为 NULL,而它应该是字符串 'tjones'?
我问的原因是因为我需要变量 $userName; 用于代码的其他部分,并且因为它是 NULL 没有其他工作,我不知道为什么?
【问题讨论】:
-
var_dump输出自身,您不需要与echo一起使用。