|
-
Sep 2nd, 2001, 05:20 PM
#1
php variable problems
ok i am trying to get in more depth in php and I was making a little program to get use to using the functions and calling them.
the problem is when I run this, it has 4 input boxes and when I put values in these boxes it is suppose to do some calculations and return the end result to the screen.
so, when it echo's part of the variables to the screen, then it calls a function that makes the calculations and then echos more variables to the screen, then runs the rest of the code by echoeing more variables to the screen. But after it goes to the function it seems that my variables aren't coming back for the next part. the last part is using a variable that is the outcome of the function I called. but it is zero once it comes back from teh function, but it is ok inside the function when it is echoed to the screen.
does that make since??
-
Sep 3rd, 2001, 12:21 AM
#2
no way it is a secret 
I fix it but I still don't understand why some variables don't go to the function.
this is part as it is too much to post the whole thing.
PHP Code:
function capacitor($c,$freq){
$p = pi();
$x1 = $myCalculator->multiply($p,2);
$x2 = $myCalculator->multiply($x1,$freq);
$x3 = $myCalculator->multiply($c,$x2);
$xc = $myCalculator->divide(1,$x3);
}
$volts = $myCalculator->multiply($i,$xc);
echo "<p>Your values are as follows<p>";
echo "<b>Volts</b> = $volts v";
echo "<br><b>Capacitor</b> = $c F";
echo capacitor($c,$freq);
echo "<br><b>Frequency</b> = " .number_format($freq) . " Hz";
echo "<br><b>Current</b> = $i Amps";
$p = $myCalculator->multiply($i,$volts);
echo "<br><b>Power</b> = $p Watts";
ok before I put the variables between the () in the function nothing was going up to it. For eg. I insert the $freq in a text box and it was not going to the function. the names were all right and I double checked everything.
-
Sep 3rd, 2001, 12:03 PM
#3
try adding this to the beginning of the function:
Code:
global $var1, $var2, $var3;
Just change $var1, etc. to the name of the variables you want the function to access. As far as I know there aren't limits to how many variables you can declare as global.
-
Sep 3rd, 2001, 12:41 PM
#4
thanks bud, I will remember that next time, since I already fixed it, or I might go back to that way and do it like that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|