Passing Variables – PHP Functions

This obviously is not a new topic. Functions and variables have been around for as long as programming has been around, and passing variables in functions are present in all programming languages. For the sake of completeness, since we just covered "include" and "require," let's take a look at functions and variables maybe from a slightly different angle.

So let's get started. This metric stuff is disorienting at times. To make things easier for folks visiting your web site, you decide to offer them an easy way to convert Celsius temperature to Fahrenheit. Since many user's may want to do this conversion, let's create a function to convert Celsius to Fahrenheit.

So here's the function.

function ConvertCelsius ( $celsius_to_convert ) {

$fahrenheit = (1.8 * $celsius_to_convert ) + 32;

return $fahrenheit;

}

You drop the function in your library.php file, and your all set. Let's use it. You have a map of a region on your web page that shows temperature in Celsius all over the map. In the upper right corner you have an input named, "celsius." labeled "Convert Celsius to Fahrenheit." with a Submit button. Yes, I know it would be nicer to click a button and convert all the readings to Fahrenheit. I'll leave that to you. For this example, you type a temperature into the box and click the "Submit" button. We'll use a form "Post" method, and on submit send it back to your controller file. Let's peak into the controller.php file.

/* Bring the inputted temp in from the form
and assign it a variable name.  */
$celsius = $_POST['celsius'];

/*  Bring in the function in the library.php file  */
include ("library.php");

/*  Call the function to do the conversion  */
$fahrenheit = ConvertCelsius ( $celsius ) ;

Now, we have some things to talk about. First, you do not have to put your function in a library file, you can put it in any file, including the same file where you use it. The function obviously needs to be declared or stated before the function is called.

You can pass more than one variable into a function. Convert ($inch, $foot, $gallon, $fahrenheit); would pass four variables into the function. However, you're only allowed to pass one variable back from the function with the return, as in "return $fahrenheit". Not to worry, the one variable, can be an array or an object, which would allow you to pass more than one variable back with a return.

Notice that the variable, "$celsius" you passed into the function does not have to be called "$celsius_to_convert" that you have in the function. It is converted to "$celsius_to_convert" by the function when in the function.

Which brings us to scope. "$celsius_to_convert" is called a local variable. It is valid only inside the function. I can not use it outside the function. The way to use the data coming back from the function is to assign it to a variable outside the function, like so: $farenheit = ConvertCelsuis ( $celsius ). We can use $fahrenheit outside the function. This concept of where a variable is valid is known as scope of a variable. Let's stop here for now.

Comments are closed.