timpearson26 Posted December 21, 2011 Share Posted December 21, 2011 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Cost Calculator</title> </head> <body> <?php // Script 10.4 - calculator.php /* this script displays and handles an HTML form. It uses a function to calculate a total from a quantity and price. */ //this function returns the calculations function calculate_total ($quantity, $price) { $total = $quantity * $price; //calculation $total = number_format ($total, 2); //formatting return $total; //return the value. } //end of calculate_total() function //check for form submisstion if ($_SERVER['REQUEST_METHOD'] == 'POST') { //form validation if (is_numeric($_POST['quantity']) && is_numeric($_POST['price'])) { //call the function and print the results $total = calculate_total($_POST['quantity'], $_POST['price']); print "<p>Your total comes to $<span style=\"font-weight: bold;\">$total.</span></p>"; } else { print '<p style="color: red;">Please make sure to enter only numbers into the calculator.</p>'; } // end of form validation } // end of if checking form has been submitted. ?> <form action="calculator.php" method="POST"> <p>Quantity: <input type="text" name="quantity" size="3" /></p> <p>Price: <input type="text" name="price" size="3" /></p> <p><input type="submit" name="submit" value="Calculate!" /></p> </body> </html> Are variables located inside of functions separate from variables located outside of functions? For example in the above code, the calculate_total() function returns the variable $total. But later on in the script, I assign $total to this: $total = calculate_total($_POST['quantity'], $_POST['price']); By doing that am I overwriting the $total variable? My guess is that the answer is no, because I tried changing the $total variable to something different, for example to $sum, like this: //call the function and print the results $sum = calculate_total($_POST['quantity'], $_POST['price']); print "<p>Your total comes to $<span style=\"font-weight: bold;\">$sum.</span></p>"; and the function still worked properly. I was just looking for a little clarification on this. Thanks. Tim Link to comment Share on other sites More sharing options...
phpRob Posted December 21, 2011 Share Posted December 21, 2011 All your doing in that above script is assigning the returned value from the function to a new variable '$total' so the result can be printed out. You might also want to check out the variable scope section a little further on in chapter 10. 1 Link to comment Share on other sites More sharing options...
timpearson26 Posted December 22, 2011 Author Share Posted December 22, 2011 You might also want to check out the variable scope section a little further on in chapter 10. Thanks. Yeah I stopped for the night just before the 'Variable Scope' section . After just reading it, it makes a lot more sense. What exactly answered my question was this: page 279: Function variables - the arguments of a function as well as any ariables defined within the function - exist only within that function and aren't accessible outside of it. also page 280: Because of variable scope, a local variable within a function is a different entity than a variable outside of the function, even if the two variables use the exact same name. Thanks. Link to comment Share on other sites More sharing options...
jabsalud Posted February 29, 2012 Share Posted February 29, 2012 hey sir i have a question why is it necessary to use return value in a function instead of using echo or print? Link to comment Share on other sites More sharing options...
Larry Posted February 29, 2012 Share Posted February 29, 2012 If the function only uses echo or print, then the only thing the function can do is send that value to the browser. If a function returns a value, then it can be used in any kind of way, including echo or print at a later point. For example, if a function calculated a shipping cost, if the function only printed that calculated value, then it couldn't be stored in a database, used in a payment process, or sent as part of an email receipt. Link to comment Share on other sites More sharing options...
janosfuster Posted May 29, 2012 Share Posted May 29, 2012 I'm not sure if I understand. If I, for example, create this function: function make_full_name($first, $last) { $name = $first . ' ' . $last; } I can't use the $name variable, unless i return it before closing the curly brackets? Link to comment Share on other sites More sharing options...
margaux Posted May 29, 2012 Share Posted May 29, 2012 That's correct. function make_full_name($first, $last) { return $name=$first . ' ' . $last; } In your calling script, call the function with the 2 arguments and store the return value in a variable. $fullname = make_full_name($fname, $lname); You can then use $fullname in the calling script to do whatever - echo to the browser, send as part of an email, insert to a database. Link to comment Share on other sites More sharing options...
Recommended Posts