I'm working on a quite simple if-else statement, which containes the strstr or strpos condition.
The code is :
if(strpos(strtolower($ofcountry),"except")){
....
}
Can you tell me what is wrong with this code?
Greetings Phil
strpos returns False when the string is not found, and 0 when the string is found in position 0. But in an if clause, 0 is also false! How can you tell one from the other? Easy: === (or !==) operator will return false only against absolute False. Not against 0. So use it like this:
if(strpos(strtolower($ofcountry),"except") !== False){
http://php.net/manual/en/function.strpos.php , check first pink Warning
Related
This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
Closed 1 year ago.
When using strpos() to find the following, there seems to be a bug.
$url = 'https://www.example.com';
if (strpos($url,'https')>0) the result is NOT found
if (strpos($url,'https',0)>0) the result is NOT found
if (strpos($url,'ttps')>0) the result is FOUND
Why is this happening?
Even though I indicate the starting position of 0, it is not finding it.
Is this a bug or is there something subtle I am missing?
Thanks
Per the manual: https://www.php.net/manual/en/function.strpos.php
Returns the position of [...] the needle [...] note that string positions start at 0, and not 1.
Returns false if the needle was not found
Warning:
This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
You need to check for strpos === false. Otherwise PHP implicitly converts the 0 to false.
The return value 0 means that the substring was found at position 0 in the searched string.
So, the check for > 0 is wrong.
Documentation says:
Returns the position of where the needle exists relative to the
beginning of the haystack string (independent of offset). Also note
that string positions start at 0, and not 1.
Returns false if the needle was not found. Warning
This function may return Boolean false, but may also return a
non-Boolean value which evaluates to false. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
https://www.php.net/manual/en/function.strpos.php
I found this, when I'm working in regex validation in PHP.
I've used preg_match function to validate a string.
I've just printed that value returned by preg_match to ensure working flow.
During that I've found a thing. But can't understand their concept.
That was preg_match() returns 1 if the match found. Otherwise, it will return 0. But sometimes my print function didn't print anything.
After I went through the PHP manual to know all of its return value...
There they posted as follows...
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
And then I used var_dump to know anything is printed or not...
This helped me to know that's a Boolean false.
But I'm curious to know why it returns boolean false, when I'm just putting " ! " (not) before the preg_match()?
Following is my code,
echo preg_match("/script/", "script") // ===> this returns 1
echo !preg_match("/script/", "script") // ===> this returns Boolean false
I think it has to return integer 0 (zero)... What is its functionality? Or did I do anything wrong in syntax?
I've tried this in the OpenCart 2.0.0.0 system administrator controller module.
The ! operator will always return a Boolean value.
For a unary ! operator the type of the result is bool. The value of the operand is converted to type bool and if it is TRUE then the of the operator result is FALSE, and the result is TRUE otherwise.
Language specification reference: Expressions
The normal conversion rules to a bool are applied to the value.
You're not echoing preg_match's return value when you put a ! in front of the function. You're using PHP's operator to determine if it loosely evaluates to true or false.
!preg_match("/script/","script") is the same as preg_match("/script/","script") == false. Note the == and not the ===. 0, null, empty string, and Boolean false will evaluate loosely to false.
I was looking into trying to revise some old code in an application that I work on. Currently the app parses out portions of a response string received from an API to determine if a request is good or if it failed. The response from the API sends back a string that contains the characters "DP" if the request was processed successfully. Right now there's a line of code in the app that is as follows:
if(stripos($result, "DP") !== false)
This is working fine now, but I can foresee an issue coming from this. stripos can return a "falsey" value even when the needle is in fact found within the haystack. Since the haystack string is zero-indexed with stripos the function will return 0 if the characters "DP" are found at the very beginning of the haystack string, which will incorrectly be read as false. This code is working now, but if for any reason the developers who maintain the API we work with decide to reformat their response, we will have problems. I was thinking of changing this to the following:
if(stristr($result, "DP") !== false)
From what I can tell this should be OK because according to php.net stristr only returns false if the needle is not found in haystack. I'm curious though if anybody has seen any problems similar to the one described above occurring with the stristr function.
0 doesn't equal false if you use === (or !==).
See this fiddle for proof: http://phpfiddle.org/main/code/nih-esg
More info on the PHP site here: http://www.php.net/manual/en/language.operators.comparison.php
Since your using !== it is a non issue, since the tripple operators checks both value and type
false != 0 : false
false !== 0 : true
<?php
$result="DP";
if (stripos($result, "DP") !== false)
{
echo stripos($result, "DP");
}
?>
Returns 0 from within brackets.
I'm using strpos to find the position of a string in another string. I first check if the string is found at all in there. Here's my line:
if (strpos($grafik['data'],$ss1)<>false && strpos($grafik['data'],$ss2)<>false && strpos($grafik['data'],$ss1) < strpos($grafik['data'],$ss2))
I check if both strings are contained and then I want the first one to be placed before the second one. In the php manual it says that strpos returns false when string is not found. However if my string starts at the zero position (strpos returns 0 since its the beginning), it seems like this statement
strpos($grafik['data'],$ss1)<>false
is false. Somehow 0==false ? How do I make the statement true when strpos returns 0 ?
From http://www.php.net/manual/en/function.strpos.php:
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.
You have to use the === operator instead of ==.
In your case, instead of using <>, use !==:
strpos($grafik['data'], $ss1) !== false
This will return TRUE if $ss1 is found in $grafik['data']
You need to check with ===. This will make sure you have exact false and not 0.
This function behaves unpredictably, so to be sure it'll have deterministic behavior use either
if(strpos($text,$string)===false)
or test it using a variable
$pos=strpos($text,$string);
if($pos===false)
This should be pretty straightforward, but I can't seem to find an explanation anywhere on how to do it.
I have a string in PHP. That string might contain within it somewhere the substring ":ERROR:". I need to find if it has that string. strpos() has worked perfectly up to this point, until today when ":ERROR:" was the first item in the string, so strpos() returned 0, so the program kept running thinking it had no error.
I don't need to replace the string, or do any manipulation to it, I just need a simple true/false answer to "does :ERROR: exist in the string?"
strpos returns false when the string is not found, so check for false instead of making an implicit condition.
if(strpos($string, ':ERROR:') !== false) {
// Do something...
}
As Anurag said in the comments, with functions like these it's always best to do a strict comparison (=== instead of just leaving out the operator or using ==) because that's a common source of bugs, especially in PHP, where many functions can return values of different types.
The PHP Manual on strpos():
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.
if (strpos($myString, ":ERROR:") !== FALSE) {
// Error!
}
You can also avoid the strpos() problems of not checking with a strict type operator by using the lesser known strstr()
if(strpos($string, ':ERROR:') !== false){
//found ':ERROR:' in string
}
"[strpos()] [r]eturns the position as an integer. If needle is not found, strpos() will return boolean FALSE."
http://php.net/manual/en/function.strpos.php
If the position is 0, then merely using == or != will evaluate 0 and false as equivalent. So use === or !== to avoid type coercion.