
01-26-06, 10:35 AM
|
|
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?
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.
|

01-26-06, 11:03 AM
|
 |
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 -
|

01-26-06, 12:29 PM
|
|
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?
|

01-26-06, 12:52 PM
|
 |
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???
|

01-26-06, 09:13 PM
|
|
Newbie Coder
|
|
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
why wouldn't the second produce any output?
|

01-26-06, 11:34 PM
|
 |
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???
|

01-27-06, 09:14 AM
|
|
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...? 
|

01-27-06, 02:29 PM
|
 |
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???
|

01-28-06, 11:14 AM
|
|
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... 
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|