PHP IF 语句的问题
嘿伙计们,现在我需要检查两个文件的内容以确保它们相同
<?php
$tk1 = file_get_contents("done.txt");
$tk2 = file_get_contents("guessed.txt");
echo $tk1."<br>";
echo $tk2;
echo "<br>";
if($tk2 == (string)$tk1){
echo "got it";
}else{
echo "aww";
}
?>
好的,done.txt 里面有whatonearth,guessed.txt 里面也有whatonearth 但它一直在回声,而且从来没有回声得到它......:s。有人可以帮忙吗? 谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来好像
done.txt
和guessed.txt
没有相同的内容。也许末尾有多余的空白,或者类似的不显眼的东西。我建议您使用var_dump
而不是echo $tk1;
和echo $tk2;
,这样您就可以准确地看到这些变量的值有。It sounds as though
done.txt
andguessed.txt
don't have the same contents. Maybe one has extra whitespace at the end, or something similarly inconspicuous. Instead of doingecho $tk1;
andecho $tk2;
I would recommend that you usevar_dump
so that you can see exactly what values those variables have.您发布的代码有效,所以我的假设是这两个文件之间存在细微的差异。
这些文件是如何创建的?它们是由用户上传的吗?由 PHP 生成?如果它们之一或两者都来自外部来源,那么您应该检查换行符的处理方式。如果一个文件使用 CR+LF 换行(即“Windows 风格”),而另一个文件只是 LF(即“Unix 风格”),则比较将失败。
The code you've posted works, so my assumption would be that there is a subtle difference between the two files.
How are these files created? Are they uploaded by a user? Generated by PHP? If one or both of them are coming from an outside source, then you should check how newlines are handled. If one file uses CR+LF for newlines (i.e. "Windows-style"), but another is just LF (i.e. "Unix-Style"), then the comparison would fail.
对 php 不太了解,但是转换不会改变 $tk1 吗?
Don't know much about php but wont the casting change $tk1?