PDA

View Full Version : ? on variables inside a function


2bz2p
06-27-2008, 01:55 PM
Greetings,

So I have this straight in my head and please let me know if I am correct or not.

Below is a snippet of code that calculates the duration of the flght and now that I understand functions I would like to clean it up and make it a function.

My question is the variables $arrival_hours, $departure_hours, $departure_minutes, $arrival_minutes, are cought using the $_POST function. Now if I use the snippet of code below these $arrival_hours, $departure_hours, $departure_minutes, $arrival_minutes, will not contain anything because its with in the function.


function duration(){
/*this is the math for the duration*/
if ($arrival_hours == "00")
{
$duration_hours = (24 - $departure_hours) ;
}
else
{
$duration_hours = ($arrival_hours - $departure_hours) ;
}

if ($arrival_minutes == $departure_minutes)
{
$duration_minutes = "00" ;
}
else if ($arrival_minutes > $departure_minutes)
{
$duration_minutes = ($arrival_minutes - $departure_minutes) ;
}
else if ($departure_minutes > $arrival_minutes)
{
$duration_minutes = ($departure_minutes - $arrival_minutes);
}
$duration_seconds = "00" ;
}
Return ;
}


thanks
2b

lux
06-27-2008, 02:13 PM
pass the variables into the function as parameters


function duration($var, $var, $var)

2bz2p
06-27-2008, 02:17 PM
O Cool that rocks, thank you.

I will feel better with the perticular page cleaned up theres like 10 if statements and a hundred different snippets of code to make sure the user cant enter something in wrong.

Let me ask this real quick

when I return it right, can I just use return and it will spit out the $duration variables or do I have to declare that it returns each Duration variable.

thanks again
2b

davidj
06-27-2008, 02:50 PM
you can pass arguments just like lux said

you can also use the global method


$var1 =1;
$var2 =2;

function(){

global $var1, $var2;


}


so $var1 and $var2 values is now available to the function

when I return it right, can I just use return and it will spit out the $duration variables or do I have to declare that it returns each Duration variable.


you need to assign the result your looking to return to a common var and return that

you currently have these being set


$duration_hours
$duration_minutes
$duration_seconds


so what you need to do is asign them to a single var before return


$duration = "$duration_hours:$duration_minutes:$duration_second s";

return $duration ;


the above would return 01:45:33 for example

2bz2p
06-27-2008, 03:13 PM
I see so when you return a variable it can only be the one variable correct.

What you put changes the sql statement but still works, cool

davidj
06-27-2008, 03:15 PM
your not returning the variable just its contents

you could use IF or SWITCH and have multiple returns if you needed

davidj
06-27-2008, 03:16 PM
What you put changes the sql statement but still works, cool


Not sure what you mean by this

2bz2p
06-27-2008, 03:18 PM
You got to be the quickest replier I have ever seen lol.

[quote=davidj;140544]you can pass arguments just like lux said

you can also use the global method


$var1 =1;
$var2 =2;

function(){

global $var1, $var2;


}


so Global basically means that the variable can be used any where regardless of restrictions.

Man the more I get into PHP the more I realize its up to your imagination and you can make it do anything.

Thanks again
2b

davidj
06-27-2008, 03:20 PM
PHP is like a virtual technical lego set but imagine the biggest box ever

2bz2p
06-27-2008, 03:21 PM
Not sure what you mean by this

Oh because the sql statement entered the duration like this


'$departure_hours:$departure_minutes:$departure_se conds',


I just need to change it to $duration.

davidj
06-27-2008, 03:24 PM
thats not a SQL statement

its the vars your using in your function concatenated together

Note: You have to use double quotes when parsing variables like this or it will just be interpreted as a literal string and will output as is

2bz2p
06-27-2008, 03:30 PM
This is the entire query,


/*Add it to the reports table*/
mysql_select_db($database_conn_abrv, $conn_abrv);
$query_input = "INSERT INTO filed_pireps (date_filled, flight_date, start_icao, end_icao, route, aircraft, other_aircraft, departure_time, arrival_time, duration, comments, pilot, flight_number, flight_type) VALUES ('$file_date', '$file_date', '$start_icao', '$end_icao', '$route', '$aircraft', '$other_aircraft', '$departure_hours:$departure_minutes:$departure_se conds', '$arrival_hours:$arrival_minutes:$arrivial_seconds ', '$duration_hours:$duration_minutes:$duration_secon ds', '$comments', '$pilot', '$flight_number', '$flight_type')";
$input = mysql_query($query_input, $conn_abrv) or die(mysql_error());
header("Location:".$cfg['domain']."member/pirep_summary.php") ;
}


and I have all the times entered into the db as $var1:$var2:$var3, in stead of just combining them first and then adding it into the db.

2bz2p
06-27-2008, 04:31 PM
So when I call the function should it look like this:

duration($departure_hours,$arrival_hours,$departur e_minutes,$arrival_minutes ) ;

or like this

duration();

Thanks
2b

davidj
06-27-2008, 04:51 PM
if your passing arguments then its the first

if your using globals then is the second

like the sig... I get quoted allot... nothing explains things better than an good analogy

2bz2p
06-27-2008, 05:02 PM
like the sig... I get quoted allot... nothing explains things better than an good analogy

Analogies are awesome and that one I like alot because I was all about legos as a kid lol.

I did that wrong I meant
When I call it which should it be? I am not using the globals yet :roll:


duration($duration) ;


or


duration();

davidj
06-27-2008, 05:08 PM
the first if your passing arguments

2bz2p
06-27-2008, 05:16 PM
I see what your saying so if my function is (like the example here) Creating a variable or something like that then you must pass that new info with in the () or else it stays with in the function

But if you are creating a function to draw something out say a navigation bar then you would just leave () them blank because it will show the end result of the function and the variables and whatnot can not be used out side of the function.

davidj
06-27-2008, 05:26 PM
here is a neat function that returns a date of your choice by adding or subtracting days, months or years. As you can see am passing 2 arguments and catching them in the function by the defined vars.


function makedates($format,$offset){

return date($format, strtotime($offset));

}

here i am assigning the result to the $date1 var. The first argument defines the format as Ymd and the second i am passing in the instruction of offset (add 1 year)
$date1 = makedates('Y/m/d','+1 year');
$date1 = makedates('Y/m/d','+1 day');
$date1 = makedates('Y/m/d','+1 month');

2bz2p
06-27-2008, 06:10 PM
It took like 20 minutes but I get it now.

Thanks man
2b

2bz2p
06-29-2008, 02:19 PM
Heres another one, if I use the Global $var, $var, $var outside a function will they work inside the function and then outside?

Thanks
2b

2bz2p
06-29-2008, 02:51 PM
sorry for the double post I couldnt edit my last,


If I make a form a function then the $_POST info has to be inside the function correct?

Thanks
2b

davidj
06-29-2008, 03:06 PM
yes

you will need to add a condition to the function call to check if the form has been submitted before the function is called

davidj
06-29-2008, 03:08 PM
no the $_POST vars dont have to be inside the function

it depends on how you write the function

2bz2p
06-29-2008, 03:49 PM
DJ,

Okay, got that part. did you see my other post about globals, if not here was my question and more.

If I place global $var in an include say settings.inc.php and then include that on the page with the function, will the variables carry through the function without having to put them in ().

Is this the correct way to write globals

globals $var1, $var2,$ect;


thanks
2b

davidj
06-29-2008, 06:26 PM
ok

this is a function with arguments
fuction myfunc($arg1,$arg2){
this is a function using globals
fuction myfunc(){

global $arg1,$arg2;



you will have to assign the vars as normal

when you include a file if you think of it like this

the line you use to include any file php sees the code of the included file as if it was written in its place

2bz2p
06-29-2008, 07:09 PM
I get it so


$var1 = 1;
$var = 2;

function add() {

global $var1,$var2;

$var3 = $var1+$var2;

return $var3;
}

add();
echo $var3;


using the above example,
the function is taking the variables 1 and 2 from outside of the function and adding them into the function because of the global command inside of the function.

then returning/spitting out $var3 which would be 3 when echoed.

sorry for my mental block on these, its mainly the variables, that are messing me up.

thanks
2b

davidj
06-29-2008, 07:18 PM
try this

$var1 = 1;
$var = 2;

function add() {

global $var1,$var2;

$var3 = $var1+$var2;

return $var3;
}

echo add();

davidj
06-29-2008, 07:19 PM
or to assign the return result to a var

$result = add();

2bz2p
06-29-2008, 07:30 PM
I see said the blind man as he peeed on his shoe,

that makes my frustration go away.

its amazing the many different ways to write functions huh, it got me seriously confused, lol.

thanks again
2b

davidj
06-29-2008, 07:34 PM
remember that the call not only calls the function it also returns the value if there is one so echo-ing the call is the same as echo-ing the result

2bz2p
06-29-2008, 07:42 PM
thanks, i have to keep that in mind.