Jump to content

[SOLVED] Why doesnt this function return the result?


JJohnsenDK

Recommended Posts

Hey

 

Why doesnt this function return the result in $returnstr? I can echo the result inside the if statement, as you can see in the code. So where is the error?

 

<?php
function f($str){
$returnstr = "Resultat: ";
if(findLastCharInString($str) == "("){
	$returnstr .= substr($str, 0, -1);
	echo $returnstr."<br>";
}else{
	echo $str."<br>";
	f(substr($str, 0, -1));
}

return $returnstr."222";

}

$test = f("Mohair sokker sort (Large)");

echo "test: ".$test;
?>

it calls f() on the inside, and returns it to the else{} statement... but you don't do anything with it so it's lost. Try changing

f(substr($str, 0, -1));

 

to

 

$returnstr = f(substr($str, 0, -1));

 

that MIGHT return what you expect, it might return something else (I don't know what you want).... at least it will give you some idea of how to refine your function (which, frankly, could probably be done more efficiently and safer);

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.