I get a project and i see a piece of code as follows:
<?php
$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 parameter make the page returning 'exists'?
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";
}
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 ";
}
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";
}?>