Forum Moderators: coopster

Message Too Old, No Replies

Passing and Declaring Variables Into a Function

         

ukgimp

11:04 am on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have created a date validation script that checks for a date in the future and that a second date is >= to the first. Quite pleased with myself as it works well :).

I wish to make it into a function and eventually put into a form validating class. I cannot seem to pass the variables correctly. It is driving me nuts. I have seen lots of examples but I cannot seem to get to the end point.

I have six variables which are the main parts of the dates split into their respective dd, mm, yyyy all labelled differently.

Do I define these in the function itself eg

function dateValidation($a, $b, $c …$n)
{
//do I need to global $a etc?

//do stuff with variables
$value = $a + $b;
}

//call function
dateValiodation();

Now do I call the variables in the function call. I am confused :(
The variables are always there so I don’t think I need to $a=”” just in case of a blank variable.
There are so many potential variations and I still may not have it all correct.

Cheers

BCMG_Scott

12:02 pm on May 21, 2003 (gmt 0)

10+ Year Member



If you define a function as follows:

function myfunction($var1,$var2){
print "do something with $var1 and $var2\n";
}

then you must call it like this:

myfunction($passvar1,$passvar2);

Now with the date function, my preference is to convert the dates to integers (aka unix timestamps) and check that way. Then you can just use basic math. You don't have to split them apart and fiddle with the pieces. Like this:

if ( strtotime($date1) > strtotime($date2) ) {
print "$date1 is greater than $date2\n";
}