Jump to content

[SOLVED] Strange function problems


sanahoria

Recommended Posts

So i have this function:

function change_get_($varname,$value,$get) {
if(strpos($get,"?") === false) { return "?$varname=$value"; } //get data empty, no ?
if(strpos($get,"?") == strlen($get)-1) { return "$varname=$value"; } //get data empty
$str = substr($get,strpos($get,"?")+1);
if(strpos($str,$varname) === false) //var not in get data
	return $str . "&$varname=$value";
//var already exists in get data
if(strpos($str, "&", strpos($str, $varname)) !== false) { //variable not last one
	$sub = substr($str,strpos($str, $varname),strpos($str,"&",strpos($str, $varname)) - strpos($str, $varname));
}
else { //variable is last
	$sub = substr($str,strpos($str, $varname));
}
$ret = str_replace($sub, $varname . "=" . $value, $str);
return $ret;
}
function change_get($varname,$value) {
change_get_($varname, $value, $_SERVER['REQUEST_URI']);
}

 

which works perfectly (btw if anyone knows of an easier way to do this without using forms please tell me). I know this because if I insert an "echo $ret" after $ret is declared it outputs the correct string.

 

however if I reference the method later in away that forces it to get to the last return statement, I get absolutely no return value.

for example if I call,

echo "hello: " . change_get("num", 5);

later in the code, when the URI is something like "test.php?num=4" the output is

Hello: 

.

Am I making a really stupid mistake?

I have tried everything I could think of (returning the str_replace w/o the $ret variable, moving the functions to an external file that i then included, etc.) but nothing would work (although in each case if I added the echo "$ret" in the function I would get my desired output. This just won't work though, I need it to return a string.

 

Thank you in advance

Link to comment
https://forums.phpfreaks.com/topic/110189-solved-strange-function-problems/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.