这是我在比较两个值的PHP代码。课程值是我从来自数据库的控制器传递的,而prev_course具有之前选择的值。虽然比较变得相同,但每次都会显示代码的else部分。正如您所看到的,第3个输出是相同的GRE和GRE,但仍然显示不相同
代码的输出就像这样
not same
prev course= GRE and from db=IELTS
not same
prev course= GRE and from db=TOFELS
not same
prev course= GRE and from db=GRE
这里是用于比较的PHP代码我已经尝试了==和===
<?php foreach($instructor_course as $courses):?>
<?php if($courses['name']===$prev_course):?>
<?php echo 'same<br/>';?><?php echo "prev course=$prev_course"." and from db=".$courses['name'] ."<br/>";?>
<?php else: ?>
<?php echo 'not same <br/>';?><?php echo "prev course=$prev_course"." and from db=".$courses['name'] ."<br/>";?>
<?php endif;?>
<?php endforeach;?>
答案 0 :(得分:2)
试试这个:
foreach($instructor_course as $courses) {
if($courses['name'] === trim($prev_course)) {
echo 'same<br/>';
echo "prev course=$prev_course and from db=$courses['name']<br/>";
} else {
echo 'not same <br/>';
echo "prev course=$prev_course and from db=$courses['name'] <br/>";
}
}