PHP IF 语句的问题

发布于 2024-11-29 20:22:46 字数 389 浏览 1 评论 0 原文

嘿伙计们,现在我需要检查两个文件的内容以确保它们相同

<?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。有人可以帮忙吗? 谢谢

Hey guys, now i need to check the contents of two files to make sure there the same

<?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";
}
?>

Okay inside done.txt is whatonearth and inside guessed.txt theres also whatonearth in it
But it keeps echoing aww, and never echos got it...:s. can somone help?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2024-12-06 20:22:46

听起来好像 done.txtguessed.txt 没有相同的内容。也许末尾有多余的空白,或者类似的不显眼的东西。我建议您使用 var_dump 而不是 echo $tk1;echo $tk2; ,这样您就可以准确地看到这些变量的值有。

It sounds as though done.txt and guessed.txt don't have the same contents. Maybe one has extra whitespace at the end, or something similarly inconspicuous. Instead of doing echo $tk1; and echo $tk2; I would recommend that you use var_dump so that you can see exactly what values those variables have.

一曲爱恨情仇 2024-12-06 20:22:46

您发布的代码有效,所以我的假设是这两个文件之间存在细微的差异。

这些文件是如何创建的?它们是由用户上传的吗?由 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.

人生百味 2024-12-06 20:22:46

if($tk2 == (字符串)$tk1){

对 php 不太了解,但是转换不会改变 $tk1 吗?

if($tk2 == (string)$tk1){

Don't know much about php but wont the casting change $tk1?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文