Current location: Hot Scripts Forums » Programming Languages » PHP » strpos Problem


strpos Problem

Reply
  #1 (permalink)  
Old 01-26-06, 10:35 AM
EndocrineFlippa EndocrineFlippa is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
strpos Problem

i am having a problem with this code. I can't seem to get it work like i want and i am clueless as to why it doesn't work...any suggestions?


Code:
<?

if (strpos("http://www.blahblah.net", "http://") == "0") {
      echo "a-ok";
} else {
      echo "not working";
}

?>
EDIT: I fixed this so it works now, but my original code still doesn't work... in my original i am using variables...but the variables are defined

Last edited by EndocrineFlippa; 01-26-06 at 10:38 AM.
Reply With Quote
  #2 (permalink)  
Old 01-26-06, 11:03 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Because strpos can return a value of zero, when you put it into the if () it gets evaluated as a boolan FALSE. The if becomes a test of is FALSE == "0", which is not true.

See this info from the PHP manual -
Quote:
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.
So, try it this way -
PHP Code:

if (strpos("http://www.blahblah.net""http://") === 0
Reply With Quote
  #3 (permalink)  
Old 01-26-06, 12:29 PM
EndocrineFlippa EndocrineFlippa is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
alright, thanks a bunch, it works now, but now i have a new question/problem in the script... the second if statement, i want to make not work...but the script just seems to skip over it? why? is it cause i need an "if else" there?

Code:
<?

if (strpos("http://www.blahblah.net", "http://") === 0) {
       echo "blahblah";
}

if (strpos("haha.net", "http://") === 0) {
       echo "nah nah";
}
Reply With Quote
  #4 (permalink)  
Old 01-26-06, 12:52 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
As written, the 2nd if statement is false and would not produce any output.

If you want the 2nd if statement to be tested only when the 1st if statement is false, you would use - elseif
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #5 (permalink)  
Old 01-26-06, 09:13 PM
EndocrineFlippa EndocrineFlippa is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
why wouldn't the second produce any output?
Reply With Quote
  #6 (permalink)  
Old 01-26-06, 11:34 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
The string "hsha.net" does not contain the string "http://" at all, and certainly not at position 0.

if (strpos("haha.net", "http://") === 0)
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #7 (permalink)  
Old 01-27-06, 09:14 AM
EndocrineFlippa EndocrineFlippa is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
what position is it at then??? i thought position 0 meant it didn't exist...?
Reply With Quote
  #8 (permalink)  
Old 01-27-06, 02:29 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
http://www.php.net/manual/en/function.strpos.php

In your 1st if statement, the 2nd string was found at position 0 in the 1st string, the strpos returned a numeric zero, hence the comparison 0 === 0 caused the if statement to execute the echo statement.

In your 2nd if statement, the 2nd string was not found and the strpos returned a boolean FALSE. The comparison FALSE === 0 causes the if statement to skip over the echo statement.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #9 (permalink)  
Old 01-28-06, 11:14 AM
EndocrineFlippa EndocrineFlippa is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
is there any reasonable way to search for a string with-in a string because strpos really sucks a lot...i changed the script to echo something if strpos("http://www.blahblah.net", "http://") == "0" or == "FALSE" or == ""...and STILL it isn't echoing on something i type in...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird mysql_query and Smarty problem Canglan PHP 3 11-15-05 08:49 PM
header redirect problem loeddie PHP 2 05-18-05 11:22 AM
Count problem kasic ASP.NET 1 10-20-04 12:23 AM
Asp and Microsoft Access 2002 problem gop373 ASP 2 10-06-04 09:13 AM
an unusual problem.... fabulosas10 PHP 0 07-16-04 02:03 AM


All times are GMT -5. The time now is 06:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.