I am having problems with strpos evaluating properly.
$status = "L";
$x = strpos($status,'L');
echo var_export($x,true);
echo "<br/>";
if (strpos($status,'L') === true) {echo "L is there!.";}
else {echo "No L Found!";}
This outputs:
0
No L Found!
With how I understand strpos and the "===" vs the "==" this should be finding the L.
What do I not understand?
strpos doesn't return true, it returns false if the string isn't found or the index if it is found.
From the official docs:
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.
You do need to perform a strict comparison. You just need to do the opposite one.
if (strpos($status, 'L') !== false) {
echo "L is there!.";
} else {
echo "No L Found!";
}
When evaluated with === true, strpos($status,'L') would have to return a literal boolean true, not just a value that evaluates to true, and as you can see in the documentation, strpos will never return that.
If you used == true instead, it would work sometimes, only when L was not the first character in the string. When it is the first character, strpos($status,'L') will return 0, which does not evaluate to true, but any other position in the string would return a positive integer, which does.
Since false is the value the function returns if the search string is not found, the only reliable way to do this is to do strict comparison against false.
Related
I'm using the following code:
if(isset($_GET['TDEnderEmail']) || htmlspecialchars($_GET['TDEnderEmail'])){
$VEENFSL = strval($_GET['TDEnderEmail']);
if(strpos($VEENFSL, "#enderadel.cf") > 0 && strlen($VEENFSL) > 5 && strlen($VEENFSL) < 22){
return true;
}
}
and although that $VEENFSL's if (strpos($VEENFSL, "#enderadel.cf") > 0 && strlen($VEENFSL) > 5 && strlen($VEENFSL) < 22) statements values are turn for the example -user#enderadel.cf- if() never return a true
Be warned that strpos doesn't work like you probably think it does: as the documentation states, strpos
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.
This means that your example string probably matches at position 0, therefore
strpos($VEENFSL, "#enderadel.cf") > 0
is false.
Therefore, you should replace that with
strpos($VEENFSL, "#enderadel.cf") !== false
I know the $a variable with the tag is not properly formatted, however that's irrelevant to the issue.
The issue is that strpos is looking for a forward slash, /, in the value of each key in the array, but it is not printing.
$a = '<a target="" href="/test/url">test';
$a_expanded = explode("\"", $a);
echo print_r($a_expanded);
foreach($a_expanded as $num => $aspect) {
echo $aspect;
if ($contains_path = strpos($aspect, '/')) {
echo $a_expanded[$num];
}
}
It echos the array and each aspect, but will not echo the string with the forward slashes when found by strpos.
if ($contains_path = strpos($aspect, '/'))
should be
$contains_path = strpos($aspect, '/');
if ($contains_path !== false)
as strpos will return 0 when the string directly starts with a / (as it does, in your case). If strpos has no match, it returns false.
if (0) and if (false) are the same. So you need to do strict comparison (=== or !==) here.
The position of found string might be 0 which is counted as false, you need to compare as ===
if (false !== $contains_path = strpos($aspect, '/')) {
echo $a_expanded[$num];
}
strpos() could either return FALSE, 0, or a non-zero value.
If the needle occurs at the beginning of the haystack, strpos() returns 0.
If it occurs elsewhere, it returns the respective position of the needle in the haystack.
If needle wasn't found in the haystack, strpos() returns boolean FALSE.
I wasn't checking for strict equality, so the if statement always returned a falsey value, causing my code to not work.
if ($contains_path = strpos($aspect, '/'))
To fix the issue, you could use !==, which compares the type and value:
if ($contains_path = (strpos($aspect, '/') !== FALSE))
For more information, check the following links:
http://php.net/strpos
http://www.php.net/manual/en/language.operators.comparison.php
Question, how come the following executed the echo:
$str = "Hello World";
if (strpos($str, 'He') !== false) {
echo 'GOOD';
}
But this doesn't:
$str = "Hello World";
if (strpos($str, 'He') === true) {
echo 'GOOD';
}
Aren't the two conditions equivalent in that they are both checking for the returned to be a boolean that is set to true? Isn't !== false the same as === true, and if not, why not?
I appreciate the clarification.
No they're not equivalent:
strpos() returns either boolean FALSE (if not found) or an integer offset value (which can be 0 if found at offset 0 and so on), but it never returns a boolean TRUE. ie., Boolean TRUE !== an INT.
The operator === compares not only value but also a datatype. If strpos finds the substring, it returns the position which is of type int. As it is not bool, the condition is not met.
I'm trying to do some validation in PHP, and one of them is checking whether there is a specific word in the inputted string or not.
The problem is, my code seem to not working when I put the specified word first.
here's the code:
$word = "aa bb cc dd";
if(strpos($word, 'aa') == false)
{
echo "wrong input";
}
but if I change the $word to either bb aa cc dd or bb cc dd aa, it works. I wonder how to fix this though.
strpos will return false if your string isn't there. Otherwise, it returns the position of your string.
In this case, 'aa' is at the start of the string, which means that it's at position 0; and 0 evaluates to false.
You need to do a boolean compare on the result:
if(strpos($word, 'aa') === false)
That's because strpos returns the position of the word, in this case 0. 0 is falsey. == does not check for identical matches, === does. So use a triple equals.
It's even in the docs.
strpos is returning 0, as 'aa' is the 0th character. As 0 == false but does NOT === false (it is not boolean), you need to use === instead of ==.
You should use the strict comparison operator, this will match against the same type, so using === will check if it's a Boolean:
if(strpos($word, 'aa') === false)
{
echo "wrong input";
}
Using == is a loose comparison, anything can be stated true (apart from true, 1, string), e.g.
"false" == false // true
"false" === false // false
The reason why it's false because it's comparing a string against a Boolean which returns false.
Because the position of aa is 0, which equals to false.
You have to use:
if(strpos($word, 'aa') === false)
Add a space before search string and find more than 0 position
if(strpos(" ".$word, 'aa') > 0)
{
echo "Found it!";
}
if (strrpos($_POST['security_data'], $OrderReference) === false ||
md5($_POST['security_data'] . $sekey) != $_POST['security_hash'])
{
return;
}
I don't understand why is strrpos in there and === "3 equals"
and what is the dot "." doing in $_POST['security_data'] . $sekey
Thank You
strrpos returns the position of the substring.
echo strrpos("Hello", "e"); // outputs `1`
. is concatenation.
echo "Hello "."There"; // outputs: 'Hello There'
=== checks type as well as equality.
var_dump(1 == true); // true
var_dump(1 === true); // false
Here's a translation to C#:
string hash = MD5.Create().ComputeHash(Request.Form["security_data"] + sekey);
if (!Request.Form["security_data"].Contains(OrderReference)
|| hash != Request.Form["security_hash"])
{
return;
}
strrpos returns false if the string isn't found (don't know which string in which, but the docs will tell you)
=== compares type as well instead of just value. This is done so php doesn't to any casting, for example 0 == false (0 represents false in php as well) but 0 !== false as 0 isn't the same type as false.
the . is the concat operator in php.
strrpos is "return position of substring within a string, starting from the right (end) side". === is the PHP strict comparison, which compares type AND value. The strpos functions CAN return a legitimate 0 as a position, which is the very start of the string. But 0 evalutes to boolean FALSE in PHP, so the === check ensures that you're looing at a real false (strrpos found nothing) and not just "strrpos found string at position zero".
The dot (.) connects between 2 strings, and the 3 equals checks if the returned value is in the same type as what it compared to
If the contents of the variable $OrderReference are not found in the POST variable security_data, or the MD5 hash of the POST variable security_data, concatenated with (that's the . operator in PHP) the variable $sekey isn't equal to security_hash, return from the function.
=== is used to ensure that the return from strrpos() is the boolean FALSE rather than the possible valid return value of 0. === is for strict type comparison.