Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-29-2012, 02:39 AM   PM User | #1
BMAN99
New to the CF scene

 
Join Date: Nov 2010
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
BMAN99 is an unknown quantity at this point
Why is this conditional statement not evaluating like I expect it too?

Hey guys,

I'm a bit stumped on this one. I would have expected the following code to echo "string contains either 123 or abc". But it doesn't! Any idea why?

PHP Code:
<?php

$string 
"123 abc";

$needle strpos($string"123");
$needle2 strpos($string"def");

    if ((
$needle === true) || ($needle2 === true)) {
        echo 
"string contains either 123 or abc";
    } else {
        echo 
"string does NOT contain either 123 or abc";
    }

?>
Am I doing something wrong? Or is there a better way to check whether a string contains either of two specific substrings?

Last edited by BMAN99; 07-29-2012 at 02:04 PM..
BMAN99 is offline   Reply With Quote
Old 07-29-2012, 03:16 AM   PM User | #2
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
If it finds the string, strpos returns the position of the string, not "true". So you could test != false, but I don't think it will ever return "true".
Code:
    if (strpos($string, "123") || strpos($string, "def")) {
...
Dave
tracknut is offline   Reply With Quote
Users who have thanked tracknut for this post:
BMAN99 (07-29-2012)
Old 07-29-2012, 04:29 AM   PM User | #3
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
tracknut is mostly correct. strpos() won't ever return an explicit TRUE. It will be FALSE or a number. So, your conditions should look more like this:
PHP Code:
$needle strpos($string"123"); 
$needle2 strpos($string"def"); 

    if (
$needle !== false || $needle2 !== false) { 
        echo 
"string contains either 123 or abc"
    } else { 
        echo 
"string does NOT contain either 123 or abc"
    } 
You want to test for explicit, "!==", not false because with PHP's loose type conversion if the substring position is 0(at the beginning of the string) it will evaluate to false with "!=" and your result will be incorrect.
Inigoesdr is offline   Reply With Quote
Users who have thanked Inigoesdr for this post:
BMAN99 (07-29-2012)
Old 07-29-2012, 02:02 PM   PM User | #4
BMAN99
New to the CF scene

 
Join Date: Nov 2010
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
BMAN99 is an unknown quantity at this point
Thanks guys! That makes sense.
BMAN99 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:28 PM.


Advertisement
Log in to turn off these ads.