Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2004-11-23 18:40:18

Remillard
Plugin Author
From: Lenexa, KS
Registered: 2004-05-16
Posts: 169

A weird little PHP thing

Has anyone noticed this:

$pos = strpos($haystack, $needle);

If needle exists in haystack, $pos will be the position (index begins at 0). Otherwise, $pos will be boolean FALSE.

Let’s assume the needle is at position 0 in haystack. So $pos = 0.

if ($pos !== false) { This will evaluate correctly as TRUE }
if ($pos === true) { This will evaluation incorrectly as FALSE }

This sort of makes sense, as $pos=0, cast as boolean will be FALSE. But it’s a strange thing. Is the only way to evaluate $pos in a yes/no sense to check if it’s not false? PHP manual suggested that using = was the correct way to test $pos because it COULD be 0, and this was going to solve it. It seems only ! works.

Remillard

Offline

#2 2004-11-23 19:03:18

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

Re: A weird little PHP thing

Yeah, you’ve got to check that it’s a boolean FALSE — which requires using <code>===</code> or <code>!==</code>.

Last edited by compooter (2004-11-23 19:03:51)

Offline

#3 2004-11-23 19:42:31

Remillard
Plugin Author
From: Lenexa, KS
Registered: 2004-05-16
Posts: 169

Re: A weird little PHP thing

Oh… I made a typo error… this is the weird part

if ($pos = = = true) { This will evaluate incorrectly as FALSE if $pos=0 }

It seems like ONLY !== false works.

Oh, no typo… textile is sucking up two equal signs…

Last edited by Remillard (2004-11-23 19:43:04)

Offline

Board footer

Powered by FluxBB