为了账号安全,请及时绑定邮箱和手机立即绑定

为什么 if 不进行比较?

为什么 if 不进行比较?

PHP
眼眸繁星 2022-05-27 13:21:28
我试图通过 if 语句比较两个数字,但它不起作用。一个数字来自 .txt 文件中的数据库。这是数字来自的数据库:Number of registrations: 6450|name|surname|email|1241241261|name|surname|email|07802634202 这是脚本:$phone = "07802634202";// Get th file from the database and convert it in an array$database = file("database.txt", FILE_SKIP_EMPTY_LINES);// Get the lenght of the database "Rows"$database_length = count($database);// Transform  in an array the string inside each row and save them in a variable for($a = 1; $a < $database_length; $a++) {  $data[$a] = explode("|", $database[$a]);}// Check if the last variable 'Number' in a row is equal to $phone foreach($data as $key => $val) {  echo "Database val: " . $val[4];  echo "Phone val: " . $phone;  // If it's equal print yep otherwise nope  if ($val[4] == $phone) {     echo "\n yep \n";   } else {     echo "\n nope \n";  }}这是控制台打印的内容:Database val: 12412412                                                                                                                                          Phone val: 07802634202                                                                                                                                           nope                                                                                                                                                           Database val: 07802634202                                                                                                                                       Phone val: 07802634202                                                                                                                                           nope    如果有不清楚的地方,请告诉我。我已经尝试了一切,但没有任何结果。谢谢你的帮助 :)
查看完整描述

1 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

由于您的数据来自各种来源,因此始终值得trim填写字段以确保删除任何前导或尾随空格。

if (trim($val[4]) == trim($phone)) {...}

只是一个小小的补充,值得添加FILE_IGNORE_NEW_LINES到调用,file()否则你将在行尾以新行结束。


查看完整回答
反对 回复 2022-05-27
  • 1 回答
  • 0 关注
  • 189 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号