function help

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
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

function help

Post by irishmike2004 »

I am creating a function (hopefully) that will convert English measure units into Metric System for my weather application. The units the function returns will be celcius, millibars, millimeters, knots, and Kilometers. Currently I have variables that work on another variable to get this done.

for example:

Code: Select all

$metricVariable = round(($Variable * foo),2);
The problem is that I have basically 4 formulas that have to be worked on and I have forgotten how to pass and retrieve information into my function.

My idea is to create the function:

Code: Select all

convertMetrics()
{
      switch here perhaps
}
Passing in the variable name to be converted, the original units, and the units to be output...

ie) convertMetrics($currentTemp, F, C)

not sure how to set this up though...

Any help is appreciated!
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Here is my attempt

Post by irishmike2004 »

I am seemingly having trouble passing variables to the function. Here is my function as written and the call to it.

The variable we are converting is $currTemp.

Code: Select all

function convertMetrics($variable, $convertFrom)
{
		
	switch($convertFrom) {
		case "F":
			$convert = round((($variable - 32) / 9) * 5),0);
			break;
		case "KM":
			$convert = round((1.609344 * $variable),0)
			break;
		case "KNOTS":
			$convert = round((.868976 * $variable),0);
			break;
		case "MM":
			$convert = round((25.4 * $variable),0);
			break;
		case "MB":
			$convert = round((33.8639 * $variable),2);
			break;
	}
	return convert;
			
}
and the call to it

Code: Select all

echo convertMetrics($currTemp, F);
What I am missing?

Thanks.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

put this at the top of your script and you will see your problems

Code: Select all

ini_set('display_errors', 1);
error_reporting(E_ALL);
you shouls always develop w/ error reporting set high so you can see your mistakes.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

Hi there:

I put the lines at the top of my script and I still see absolutely nothing in my browser???


Not sure what is up.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

well, maybe you have fatal errors before php gets to those 2 lines(like maybe in the parent script, if your using include() for example)
it should be the very first thing php parses.

the errors is see are:
1- when you call the function, you did not use quotes around F
2-you forgot a $ on your return statement in the func definition


still, some output should be produced(the echo should output the string 'convert') .
so you must have other errors not related to the code you posted.
or maybe you have the whole thing in a conditional statement that is not evaluating to true.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Tried that

Post by irishmike2004 »

Here is the function again, I did correct those errors ;-)

If I comment it out, the script runs fine (as always) so the problem is within the function obviously.

Function:

Code: Select all

function convertMetrics($variable, $convertFrom)
{
		
	switch($convertFrom) {
		case "F":
			$convert = round((($variable - 32) / 9) * 5),0);
			break;
		case "KM":
			$convert = round((1.609344 * $variable),0);
			break;
		case "KNOTS":
			$convert = round((.868976 * $variable),0);
			break;
		case "MM":
			$convert = round((25.4 * $variable),0);
			break;
		case "MB":
			$convert = round((33.8639 * $variable),2);
			break;
	}
	return $convert;
			
}
call to the function

Code: Select all

echo convertMetrics($currTemp, "F");
Both of these are commented out right now and the script runs... if I uncomment the lines, fatal error

What could be up?

Can you not pass a variable as an argument, if not how to do you get the value into the function?

Thanks,
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

changed the function in one way... now first case is "C" for Celcius and the variable is called convertTo since that is essentially what it is supposed to do :-)

What I am trying to do is send it a variable like $currTemp which is in Fahrenheit and have the return value in Celcius for example, that is what the first case in the switch statement inside the function is supposed to do... apparently I am having issues with sending variables into the function though ... I am totally lost here. By all that is reasonable, it SHOULD be working.

Thanks,
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

Solved it folks:

It appears I was missing a "(" on my first case round statement... that always can make a bad day :-)

Thanks for your help.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

you really should get error_reporting turned on somehow.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

have it on, thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

irishmike2004, you have 62 posts. You should know by now that you are supposed to use

Code: Select all

bbTags when posting php code.  If you don't understand, click the link in my signature.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Thanks for the pointer

Post by irishmike2004 »

Actually no one ever said anything. I will use PHP instead in the future.

Sorry.
Post Reply