Page 1 of 1

return value from a function - not working

Posted: Sun Jan 03, 2010 8:44 pm
by jimandy
I have read and re-read tutorials on user defined functions and how to pass values to and from but must have missed something. This code returns an error rather than the string that it reverses. Would appreciate some help.

Code: Select all

<?php
/*  function to reverse characters in a string */
//
function nameReverse($name){
$newname=strrev($name); 
return $newname;          //(should return "miJ") 
}
//
$name="Jim";
nameReverse($name); 
print "the new name is". $newname;
?>

Re: return value from a function - not working

Posted: Sun Jan 03, 2010 9:11 pm
by Christopher
A return value has to be returned to a variable:

Code: Select all

$newname = nameReverse($name);

Re: return value from a function - not working

Posted: Mon Jan 04, 2010 8:21 am
by jimandy
Thanks Christopher. That solved my problem.