Muhammad Ahmed
Muhammad Ahmed

Reputation: 56

same String values in PHP are not equal?

I am comparing two same string values in PHP but the result says they are not equal. here is the code

 $s = $fname;
 $t = $temp->get_teacher_name()." ";
 echo "<br/> s = $s<br/>";
 echo "<br/> t  = ".$temp->get_teacher_name()."<br/>";

 echo var_dump($s)."<br/>;

 echo var_dump($t);
 if($s == $t)
 {
 echo"<br/>Matching<br/>";
 }

The $fname is read from a file using "fgets" and i think it has a extra space at end because of that. Here is the result.

enter image description here

The result says they are not equals.

Upvotes: 0

Views: 621

Answers (1)

Tigran
Tigran

Reputation: 674

You can use trim() to remove whitespace from beginning and end of string.

$s = trim($fname);
$t = trim($temp->get_teacher_name());

if ($s == $t) {
    echo "<br/>Matching<br/>"; 
}

Upvotes: 2

Related Questions