这是我比较这两个值的php代码。课程值是我从来自数据库的控制器传递的值,prev_course具有之前选择的值。虽然一次比较变得相同,但每次都显示代码的其他部分。正如你所看到的,第三个输出是相同的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;?>
试试这个:
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/>";
}
}