无法比较 PHP 中 IF 语句中的两个字符串
Unable to compare two strings in IF statement in PHP
好的,所以我一直在尝试为需要从 2 XML 文件获取信息并比较它们的 Web 应用程序构建服务器端,一次一个元素。出于某种原因,除了比较 2 个节点之外,我的代码将执行所有操作。
代码:
function getCoursesByRole($role){
s("role is ". $role);
$xml = getxml(); # get the XML file
$roles = getRoles();#load the roles file
$modules = array();
foreach($roles->children() as $project){
s($project->name);
if($project->name == $role){
s("found:". $role);
$courses = $project->courses->children();
#echo "courses ". $courses;
foreach($courses as $theCourse){
foreach($xml->children() as $course){
s("looking for $theCourse.... found ".$course->name);
if($theCourse == $course->name){
s("found $theCourse");
array_push($modules, array('name' => $course->name));
break;
}
}
}
}
}
#echo array_values($modules);
return $modules;
}
从输出来看我认为问题是逻辑问题。
role is core
core
found:core
looking for Fire Safety.... found Administration of Medication
looking for Fire Safety.... found An Introduction to GIRFEC
looking for Fire Safety.... found Equality and Diversity
looking for Fire Safety.... found Fire Safety
looking for Fire Safety.... found Food Safety
looking for Fire Safety.... found Infection Control
looking for Fire Safety.... found Introduction to Health and Safety
looking for Fire Safety.... found Introduction to Safer Handling
looking for Fire Safety.... found Managing Volunteers
looking for Fire Safety.... found QStar
我只展示了一个迭代 s("looking for $theCourse.... found ".$course->name);
它变得很长
如果有人能看到解决方案,我将不胜感激:)
Its always good idea to trim string with trim() function while using as condition if(trim($a) == trim($b)). - offshore
这条评论回答了我的问题。更改此行后:if($theCourse == $course->name){
到此 if(trim($theCourse) == trim($course->name)){
代码运行完美。
好的,所以我一直在尝试为需要从 2 XML 文件获取信息并比较它们的 Web 应用程序构建服务器端,一次一个元素。出于某种原因,除了比较 2 个节点之外,我的代码将执行所有操作。
代码:
function getCoursesByRole($role){
s("role is ". $role);
$xml = getxml(); # get the XML file
$roles = getRoles();#load the roles file
$modules = array();
foreach($roles->children() as $project){
s($project->name);
if($project->name == $role){
s("found:". $role);
$courses = $project->courses->children();
#echo "courses ". $courses;
foreach($courses as $theCourse){
foreach($xml->children() as $course){
s("looking for $theCourse.... found ".$course->name);
if($theCourse == $course->name){
s("found $theCourse");
array_push($modules, array('name' => $course->name));
break;
}
}
}
}
}
#echo array_values($modules);
return $modules;
}
从输出来看我认为问题是逻辑问题。
role is core
core
found:core
looking for Fire Safety.... found Administration of Medication
looking for Fire Safety.... found An Introduction to GIRFEC
looking for Fire Safety.... found Equality and Diversity
looking for Fire Safety.... found Fire Safety
looking for Fire Safety.... found Food Safety
looking for Fire Safety.... found Infection Control
looking for Fire Safety.... found Introduction to Health and Safety
looking for Fire Safety.... found Introduction to Safer Handling
looking for Fire Safety.... found Managing Volunteers
looking for Fire Safety.... found QStar
我只展示了一个迭代 s("looking for $theCourse.... found ".$course->name);
它变得很长
如果有人能看到解决方案,我将不胜感激:)
Its always good idea to trim string with trim() function while using as condition if(trim($a) == trim($b)). - offshore
这条评论回答了我的问题。更改此行后:if($theCourse == $course->name){
到此 if(trim($theCourse) == trim($course->name)){
代码运行完美。