Reputation: 13
I get a project and i see a piece of code as follows:
$orderby = $_REQUEST['orderby'];
if (strpos($orderby, 'd') === true) {
echo "exists";
} else {
echo "not ";
}
In any case, I input 'd' or others parameters the page always returning 'not'. So, how to input correct parameters to make the page return 'exists'?
Upvotes: 1
Views: 894
Reputation: 780909
strpos()
can never return TRUE
. If the string is found it returns the position. If the string is not found it returns FALSE
. So you should compare with FALSE
, not TRUE
.
if (strpos($orderby, 'd') === false) {
echo "not exists";
} else {
echo "exists";
}
Upvotes: 4
Reputation: 65274
if strpos
finds a match, it won't give back true
but the offset - so your strpos($orderby,'d')===true
is never hit.
Try this:
<?php
$orderby=$_REQUEST['orderby'];
if($o=strpos($orderby,'d')===false){
echo "not ";
}else{
echo "exists at offset $o";
}?>
Upvotes: 1
Reputation: 57121
Your test is not saying that it is returning false
, just that strpos()
never returns a value of a boolean true
. Instead it will return an integer with the position of the string found. Normally the check would be
if(strpos($orderby,'d') !== false){
echo "exists";
}else{
echo "not ";
}
Upvotes: 2