logixmeister
logixmeister

Reputation: 23

PHP pass by reference

I have the following code. I am expecting the check method hasQuerystring() to pass the modified url ..

However, the result shows that the hasQuerystring always returns false, meaning, the parameter is not pointing to the modified url..

Here is the code:

private function __addParams(&$url, $params, $addedParams = null) {
    if(!empty($addedParams)) {
        foreach($params as $param => $val) {
            if(!in_array($param, $addedParams)) {
                $url .= ($this->__hasQuerystring($url))? "&{$param}={$val}" : "?{$param}={$val}" ;
            }
        }
    } else {
        foreach($params as $param => $val) {
            $url .= ($this->__hasQuerystring($url))? "&{$param}={$val}" : "?{$param}={$val}" ;
        }
    }
    return $url;
}   

private function __hasQuerystring($url) {
    return strpos('?',$url);
}

[EDIT]

This is the value of url before being returned

https://mobile.domain.com/en/#/#m=2&eid=123?param1=9675?param2=23d9dj8rsaE3dG1

Upvotes: 0

Views: 115

Answers (2)

machineaddict
machineaddict

Reputation: 3236

Replace

return strpos('?',$url);

with

return strpos($url, '?') !== FALSE;

Always check the manual.

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.

That's why is always returning FALSE.

Also, if you don't use the result from __addParams, you can remove return $url;

Later edit:

I replaced return strpos('?',$url) !== FALSE; with return strpos($url, '?') !== FALSE; as Erik pointed.

Upvotes: 0

Erik
Erik

Reputation: 3636

With strpos you pass the thing to search in first, then the thing to search for next. See the documentation: http://php.net/manual/en/function.strpos.php

return strpos($url, '?') !== FALSE;

Upvotes: 2

Related Questions