Jump to content
Larry Ullman's Book Forums

Chapter 10 Question About Variables Inside Of Functions


Recommended Posts

<!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 post
Share on other sites

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 post
Share on other sites
  • 2 months later...

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 post
Share on other sites
  • 2 months later...

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...