return value from a function - not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

return value from a function - not working

Post 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;
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: return value from a function - not working

Post by Christopher »

A return value has to be returned to a variable:

Code: Select all

$newname = nameReverse($name);
(#10850)
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Re: return value from a function - not working

Post by jimandy »

Thanks Christopher. That solved my problem.
Post Reply