PHP 变量 var_dump = NULL?
PHP variable var_dump = NULL?
我有一个 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);
>?
它回显 $userName
显示 'tjones' 但是 var_dump 显示它的输出为 NULL。
为什么 var_dump 将 $userName
变量显示为 NULL,而它应该是字符串 'tjones'?
我问的原因是因为我需要变量 $userName;
用于代码的其他部分并且因为它是 NULL 没有其他任何东西在工作,我不知道为什么?
您需要修改 fileRead2.php 以使用 return
而不是 echo
:
<?php
function fileRead2() {
global $fh, $line;
$fh = fopen('username.txt','r');
$lineReturn = "";
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
$lineReturn = $line;
}
fclose($fh);
return $lineReturn;
}
?>
echo
用于将信息发送到标准输出 - 这意味着如果您在终端中 运行 一个 php 脚本并使用 echo,它将是发送到终端(假设您没有将标准输出重定向到其他地方);如果您使用 php 脚本生成网页内容,它会将信息输出到浏览器(这是一种简化)。
另一方面,Return 在函数内部用于将信息发送到函数外部的代码块。因此,在您的情况下,您希望函数 fileRead2 从 username.txt 文件中读取,然后 return 第一行(用户名),以便您可以设置函数外部的变量。为此,您必须使用 return
.
另外请注意,如果您除了将 $line 变量设置为 fgets 输出之外没有在该行上执行任何其他 "work",并且用户名位于 username.txt 的第一行文件,那么你不需要 while 循环。相反,您可以只执行 $line = fgets($fh);
,然后当然关闭文件和 return $line
变量。
您的函数 fileRead2()
没有 return 子句,因此它 return 类型为 void。而 var_dump()
的 return 类型也是 void.
所以
$userName
为空,所以 echo $userName
什么都不输出。
var_dump($username)
将输出 NULL。
echo var_dump($userName)
什么都不输出。
而在您的代码中,readfile2 只需添加 return $line;
即可 return false
我有一个 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);
>?
它回显 $userName
显示 'tjones' 但是 var_dump 显示它的输出为 NULL。
为什么 var_dump 将 $userName
变量显示为 NULL,而它应该是字符串 'tjones'?
我问的原因是因为我需要变量 $userName;
用于代码的其他部分并且因为它是 NULL 没有其他任何东西在工作,我不知道为什么?
您需要修改 fileRead2.php 以使用 return
而不是 echo
:
<?php
function fileRead2() {
global $fh, $line;
$fh = fopen('username.txt','r');
$lineReturn = "";
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
$lineReturn = $line;
}
fclose($fh);
return $lineReturn;
}
?>
echo
用于将信息发送到标准输出 - 这意味着如果您在终端中 运行 一个 php 脚本并使用 echo,它将是发送到终端(假设您没有将标准输出重定向到其他地方);如果您使用 php 脚本生成网页内容,它会将信息输出到浏览器(这是一种简化)。
Return 在函数内部用于将信息发送到函数外部的代码块。因此,在您的情况下,您希望函数 fileRead2 从 username.txt 文件中读取,然后 return 第一行(用户名),以便您可以设置函数外部的变量。为此,您必须使用 return
.
另外请注意,如果您除了将 $line 变量设置为 fgets 输出之外没有在该行上执行任何其他 "work",并且用户名位于 username.txt 的第一行文件,那么你不需要 while 循环。相反,您可以只执行 $line = fgets($fh);
,然后当然关闭文件和 return $line
变量。
您的函数 fileRead2()
没有 return 子句,因此它 return 类型为 void。而 var_dump()
的 return 类型也是 void.
所以
$userName
为空,所以 echo $userName
什么都不输出。
var_dump($username)
将输出 NULL。
echo var_dump($userName)
什么都不输出。
而在您的代码中,readfile2 只需添加 return $line;
即可 return false