Php 为什么if不';你不做比较吗?
我试图通过if语句比较两个数字,但它不起作用。 一个数字来自.txt文件中的数据库 这是数字来源的数据库:Php 为什么if不';你不做比较吗?,php,file,comparison,Php,File,Comparison,我试图通过if语句比较两个数字,但它不起作用。 一个数字来自.txt文件中的数据库 这是数字来源的数据库: Number of registrations: 64 50|name|surname|email|12412412 61|name|surname|email|07802634202 以下是脚本: $phone = "07802634202"; // Get th file from the database and convert it in an array $database
Number of registrations: 64
50|name|surname|email|12412412
61|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";
}
}
如果有什么不清楚的,请告诉我。我什么都试过了,没有结果
感谢您的帮助:)由于您的数据来自不同的来源,因此始终值得对字段进行加密,以确保删除任何前导空格或尾随空格
if (trim($val[4]) == trim($phone)) {...}
只是一个小小的添加,值得在调用中添加
FILE\u IGNORE\u NEW\u行
,否则您将以新行结尾。旁注:我希望这些不是实际的电子邮件地址。编辑:现在已被删除。是否有特殊原因使您不能仅使用数据库进行此操作?是否值得在其中插入trim()
-if(trim($val[4])==trim($phone)){
@FunkFortyNiner抱歉,我意识到太晚了,我已经更改了它们。我正在学习PHP,所以我从零开始制作一个简单的数据库来提高我的技能。尝试使用var_dump($val[4]);var_dump($phone)
而不是echos…来验证数据类型和内容。显示了什么?
if (trim($val[4]) == trim($phone)) {...}