Is it ok to use substr and strpos on the array value, simultaneously?
Well to answer this question we can just look up the manual and see what each returned and passed value means. As for the types we are perfectly fine because the
substr function will accept an int (or something that can be valued as one) and we are returning an int from
As per the logical meaning, assuming
$params[count($params)-1] actually exists, we are getting a position of the first occurrence of
? in the string.
But we have to notice that
strpos might return false if no occurrence of the symbol is found, therefore leading to a problem in your script.
I suggest you to check for that option:
$e = strpos($params[count($params)-1],'?');
if ($e === false) $e = 0;
And then call:
$method = substr($params[count($params)-1],0,$e);
Is this approach of forming the URL and extracting the paramters from it correct?
Well it has few bugs we should discuss.
First, every occurrence in the form of
$params[count($params)-x] may trigger an undefined index error if the count of
$params is actually 0 (or 1). Therefore I suggest you to check for the option where the string is way shorter than you expect:
if (count($params) < 2) // what do we do?
On a side note, you probably want to store the value of
count($params) instead of calling it every time (it's not likely gonna change). (
$i = count($params)).
Second, assuming we are expecting a well formed string in the form of (
[x] means x is optional):
You might want to take a look at
$_SERVER['PATH_INFO'] which will remove the query string for you:
Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.
Notice: it will work also with URL rewriting because the actual filename will be inserted anyway into the request.