odd php behaviour

For some reason, the following code doesn’t work, I’m not sure why, it’s function should be self explanitory.

$gender = "male";

if ($gender != "male" || $gender != "female") echo 'error';
else echo "all good";

This code always returns false even with the gender variable set to male or female.

Look at the logic again. If it helps, substitute “male” where you see $gender.

$gender = "male";

if ($gender != "male" || $gender != "female") echo 'error';
else echo "all good";

becomes


if ("male" != "male" || "male" != "female") echo 'error';
else echo "all good";

See the problem? I think you wanted &&. Or use De Morgan’s theorem:

$gender = "male";

if ($gender == "male" || $gender == "female") echo 'all good';
else echo "error";

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You should almost definitely be using ‘&&’ instead of ‘||’ since you
probably want to error when NEITHER value is used. So it’s a logic
error. You want to error when it’s not mail and it is not female… as
it is you will always error because it will always be either not mail or
not female.

Good luck.

Crazy PotHead wrote:
> For some reason, the following code doesn’t work, I’m not sure why, it’s
> function should be self explanitory.
>
>
> PHP code:
> --------------------
> $gender = “male”;
>
> if ($gender != “male” || $gender != “female”) echo ‘error’;
> else echo “all good”;
> --------------------
>
>
> This code always returns false even with the gender variable set to
> male or female.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIpLc/3s42bA80+9kRAgQOAJsEFp11ivJRtP9K9mJ3naXiColeDwCeNDYz
AziKCxOBk/vlAeaA07R/Vio=
=V+Bf
-----END PGP SIGNATURE-----

Thanks for all your responses so quickly, I decided to go with the && solution so I don’t have to reformat my code… I really should have been able to figure this one out on my own, but unfortunately I haven’t touched php for use in a client project for over a year now.

Anyways, Thanks again!!

It would have been the same in any other language. :slight_smile:

You’re absolutely right :stuck_out_tongue: , I really have to apologize here, I’ve been having an off day. But on a semi-related side note, it really feels odd in general switching back to php after doing the majority or my programing in ruby (only time I touched php was installing/configuring a few wikis), makes me realize how much easier things are in ruby :slight_smile:

Anyways, thanks again for your help!lol!

Your code will be still wrong, or behave in a way you dont expect

you have to either use the identity operators “!==” or strcmp()


(strcmp($gender, 'male') === 0)

or


($gender !== 'male')

to practically understand the problem try this:


var_dump((0 == 'male'));

you expect that to return FALSE right ? as zero is not equal to ‘male’ :stuck_out_tongue:

it returns TRUE rotfl! and that’s the correct behaviuor.

whe you compare an integer with an string, the string is internally converted to integer :wink: and then compared.

Be careful with the loosely typed nature of PHP, you can shoot yourself in the foot easily. :wink:

I was going to say that the code did work… but then I looked back to that section of my code and saw that I had already put something like what you said.

Here’s my production code:

if (empty($gender) || ($gender !== "male" && $gender !== "female")){$gender_err = 1; $gen_error = 1;}

For anyone who’s curious what $gen_error is, it’s just a general error flag.