View Single Post
Old 09-02-2007, 01:58 AM  
sortie
Confirmed User
 
sortie's Avatar
 
Industry Role:
Join Date: Mar 2007
Posts: 7,771
Quote:
Originally Posted by GrouchyAdmin View Post
Not quite. For PHP, anything that isn't 0 is considered to be legitimate. I used a ternary operator because I love 'em.

Another way of writing it would be if ($test != FALSE) { echo "Found." } else { echo "Not found." }

See the link on strpos above; my shorthand was a little difficult to read if you're not familiar with the nuances of PHP, but it's valid.

Code:
<?php
$data="hello";
$testfor="h";
$test=strpos($data, $testfor);
echo ($test != FALSE) ? "'$testfor' found in '$data'.\n" : "'$testfor' not found in '$data'.\n";
?>
It's precisely the same, only with more text in it. As noted, 0 has the same value as FALSE, so unless you explicitly test for it, you can have nasty results.

It's precisely the same in the sense that both methods are completly wrong.

If you program in C, then you know that strpos is not a boolean function.
It MAY return a boolean value but it will also return an interger which is 0 ,
which is confused as boolean zero. But, in this fuction, zero is not a boolean value yet you tried to evaluate it at as such.

You have to tell php that you want to perform an incongruent comparison by using the "===" operator.

http://en.wikipedia.org/wiki/Congruent

See zero and FALSE are not EQUAL but the are congruent!
The "===" says "ignore congruence" and find exact equivalency to
FALSE.

See you can read all the stuff you want on the internet but it obvioulsy can't replace my education that a boolean function returns either zero or one.
If it returns anything else then don't do a boolean test on it.

I didn't know php had "===" but I still would have gotten it right because the code you did is just wrong wrong wrong wrong wrong and I would have never written that, because I know what a boolean value is.

I never wrote even one line of php code and I saw that right away in your example. But yet you still can't see that.

http://us2.php.net/strpos
Quote:
Originally Posted by http://us2.php.net/strpos
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

The manual tells you that.
I told you that.

I never read the manual until 5 mins ago and already knew that.

But you don't want to hear that.

Which proves one thing.

Progammers may not be incompetent, but they sure are
Fucking Hard Headed



So I back down on my "self taught programmer can write good code" statement.

Go to fucking college like I did.
__________________
sortie is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote