deltanovember
deltanovember

Reputation: 44051

Why does my PHP string comparison fail?

I have the following snippet of code

if ($summary == "CFD funding Interest Paid" ||
    $summary == "Commissions" ||
    $summary == "Closing trades") {
    print $summary.",".$date.",".$reference.",".$description.",".$amount."<br>";  
}
else {
    print $summary."*<br>";
}

It outputs the following

Commissions*
Commissions*
Closing trades*
Commissions*
Closing trades*

How come the strings do not appear to be matching?

Upvotes: 3

Views: 3552

Answers (3)

Ali
Ali

Reputation: 3666

Use the strcmp(str1, str2) function instead.

Upvotes: 7

powtac
powtac

Reputation: 41050

Add trim() before the if (), it removes unvisible chars like whitespace...

$summary = trim($summary);
if ($summary == "CF...

Upvotes: 6

Mr47
Mr47

Reputation: 2655

Perhaps you have leading whitespace? You could trim() that away to see if it helps?

Upvotes: 9

Related Questions