I think you're confusing the echo
keyword and the return
keyword.
Echoing a value will output it from the program. This is similar to printing values as you would do in other languages. Returning a value will set the function call's return value and exit the function, which is what you want here.
The reason you are getting the dollar amount before you are getting the text is because you are echoing the value when the function gets called. After that, PHP will automatically return nothing at the end of your function since you have not specified a return value. It will then concatenate "Your new sun-total is: "
with an empty string and echo that as well.
You can fix this by rewriting your money
function to look like this:
function money($num){
return "$" . number_format($num , 2 , "." , ",");
}