This may be interesting to you

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prathyush
Forum Newbie
Posts: 1
Joined: Mon Jul 20, 2009 3:41 am

This may be interesting to you

Post by prathyush »

Why does the second return success do you think?

Code: Select all

 
$txnResponseCode = "E";
 
if ($txnResponseCode != "0") {
 
 echo 'fail<br>';
 
 
} else {
 
 echo 'success<br>';
 
}
 
if ($txnResponseCode != 0) {
 
 echo 'fail<br>';
 
 
} else {
 
 echo 'success<br>';
 
}
Last edited by Benjamin on Mon Jul 20, 2009 3:43 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: This may be interesting to you

Post by Benjamin »

Because the value of $txnResponseCode is true when compared to a boolean value of false.

You can cast the data types.

Code: Select all

 
if ((string) $txnResponseCode !== (string) 0) {
 
} else {
 
}
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: This may also be interesting to you

Post by jackpf »

I had problems with this, when comparing a variable's value to 0 with in_array().

No matter what the value was, it would always return true when comparing to 0.

After reading the manual, it turns out that in_array() has a third parameter [, bool $strict], which, when set to true, also checks datatypes.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: This may also be interesting to you

Post by califdon »

jackpf wrote:I had problems with this, when comparing a variable's value to 0 with in_array().

No matter what the value was, it would always return true when comparing to 0.

After reading the manual, it turns out that in_array() has a third parameter [, bool $strict], which, when set to true, also checks datatypes.
WHAT?!! You READ THE MANUAL?!! That's heresy! 8O

To: prathyush

Please use meaningful Subject lines for posting in this forum. The Subject line should indicate what the post is about, not a general comment, like "This may be interesting to you" or "Please help me!"
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: This may also be interesting to you

Post by jackpf »

califdon wrote:
jackpf wrote:I had problems with this, when comparing a variable's value to 0 with in_array().

No matter what the value was, it would always return true when comparing to 0.

After reading the manual, it turns out that in_array() has a third parameter [, bool $strict], which, when set to true, also checks datatypes.
WHAT?!! You READ THE MANUAL?!! That's heresy!
I know...it's hard to believe that some people actually read the manual before asking for help :dubious:
Post Reply