...

View Full Version : How do I pass a variable out of a function?



JohnDubya
02-27-2007, 09:06 PM
I've designed some validation functions for my forms, and they're working awesome. The only thing is that I'm having a really hard time figuring out how to get variables that are declared within the function OUT of the function to the page the function is used. Here's one of the functions:


function CheckLength($variable, $max_length) {
//check if variable is too long
if (strlen($variable) > $max_length) {
$Error_Stat = 1;
$Message = Error('"' . $variable . '" is too long. Please enter ' . $max_length . ' or less characters.');
return FALSE;
}
}

Pretty simple. Then I call it like this:


$first_name = $_POST['first_name'];

$first_name = CheckLength($first_name, 20);

The function works, but when I try to check against $Error_Stat and print the $Message, nothing happens. I've already read that this is because the variables declared in the function stay local, but I couldn't find out how to get around this other than using global, which I've heard is a bad idea.

So how do I get $Error_Stat and $Message out of the function and to the page it's on without using global?

Spookster
02-27-2007, 10:04 PM
Making a variable global in scope is not in itself bad. In your case since you are likely not designing a major application I think it is perfectly fine. If you were designing this using object orientation which you would want to if this were going to be a large app then you would want to worry more about how you handled the scoping of your variables. Just declare it outside the function and you will be fine.

And if you really wanted to since you said you have multiple form validation functions you could just go ahead and design a form validation class and work with it that way. Working with objects is much nicer than dealing with random scraps of codes and functions. Would also give you practice if you ever design any larger applications.

JohnDubya
02-27-2007, 10:14 PM
Some great advice, especially on learning objects/classes. I will eventually take a trek on that endeavor. Thanks Spook! :)

Here's the issue I'm having with declaring the two variables as global. Normally when I set $Message, the variable gets cleared on the next visit to the page. So basically, normally I just simply put:


<?=$Message?>

And if $Message is blank, it does nothing, but if $Message has something, it prints it. Then, the next time I go to the page, $Message is blank again (unless it's been set by something again).

But, as you know, when I set $Message as global and echo it on the page, it will continue to be the global value until I change it or unset it...it doesn't clear itself on each pass to the page. So I'm finding that now, I have to use:


<?=$Message?>
<?php unset($Message);

Which just seems messy. Is there something I'm missing here?

chump2877
02-27-2007, 10:15 PM
Here's one way:


<?

$first_name = "Joe";

if (CheckLength($first_name, 20) !== true)
{
$error = CheckLength($first_name, 20);
die($error['Error_Stat'].": ".$error['Message']);
}

$first_name = "JoeJoeJoeJoeJoeJoeJoeJoeJoeJoeJoeJoeJoeJoeJoe";

if (CheckLength($first_name, 20) !== true)
{
$error = CheckLength($first_name, 20);
die($error['Error_Stat'].": ".$error['Message']);
}



// functions

function CheckLength($variable,$max_length)
{
//check if variable is too long
if (strlen($variable) > $max_length)
{
$error['Error_Stat'] = 1;
$error['Message'] = '"' . $variable . '" is too long. Please enter ' . $max_length . ' or less characters.';
return $error;
}
else
{
return true;
}
}

?>


Edit: If you have many functions/methods, and you want to output all of the errors at once, or store them all in the same array, try some OOP like Spookster said...

Fumigator
02-27-2007, 10:35 PM
I don't get why you are assigning the result of your CheckLength() function to $first_name.

Anyways... I like to keep an error array like Chump mentioned and I like to make it global to all functions. It not encapsulated, sure, which potentially can lead to problems, but limited use of global can be really handy.

aedrin
02-27-2007, 10:50 PM
This is what I do for all functions that I write that produce more than 1 variable.



function validate($data) {
$valid = false;
$message = 'The data is not right';
return array('valid' => $valid, 'message' => $message);
}

$result = validate('this is some data');
if (!$result['valid']) {
echo 'Error: ' . $result['message'];
}

GJay
02-27-2007, 10:57 PM
yet another approach is to pass $message into the function by reference:



function checkLength($string,$max_length,&$message='') {
if(string is too long) {
$message = 'String is too long';
return false;
}
return true;
}

//usage:
$string='a long string'
$check = checkLength($string,3,$message);
echo $message; //outputs 'String is too long'



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum