Returning values from a function

Hey everyone,

I'm a relative noob to php, and I'm trying to write a function that validates a number submitted from a form, then returns whether or not the number submitted was in the proper form or not. This is not a regular expression check, the number must be checked against a formula. My problem is that my calling script doesn't seem to recognize the returned value.

Here is my function script:

[code:1]<?php # custom functions

// validate ABA Routing Numbers
function validate_routing_number($routeNum) {

// break into individual characters
$routingChars = chunk_split($routeNum);

// concatenate first two characters
$routePrefix = $routingChars[0] . $routingChars[1];
echo '<p>' . $routePrefix . '</p>';

// prefix must be between 01-12 or 21-32
if ( (($routePrefix > 0) && ($routePrefix < 13)) || (($routePrefix > 20) && ($routePrefix < 33)) ) {

// check against ABA algorithm

// multiply individual characters according to algorithm and sum results
$routeCheck = ($routingChars[0] * 3) + ($routingChars[1] * 7) + ($routingChars[2] * 1) + ($routingChars[3] * 3) + ($routingChars[4] * 7) + ($routingChars[5] * 1) + ($routingChars[6] * 3) + ($routingChars[7] * 7) + ($routingChars[8] * 1);
echo '<p>' . $routeCheck . '</p>';

// must be evenly divisible by 10
if (fmod($routeCheck, 10) > 0) { // number is not valid
$routeNumValidated = FALSE;
echo "<p>Must be evenly divisible by 10.</p>";
} else {
$routeNumValidated = TRUE;
}

} else { // first two characters are not valid
$routeNumValidated = FALSE;
echo "<p>First two characters are wrong.</p>";
}

return $routeNumValidated;

} // end of function validate_routing_numbers()

?>[/code:1]

And here is the script that calls it:

[code:1]<?php # test routing number form validation

// set page title and html header
$page_title = "Test Routing Number";
include_once('includes/header.html');
require_once('includes/functions.inc');

// validate form data
if (isset($_POST['submit'])) {

// create empty variable for message
$msg = NULL;

if (strlen($_POST['routingNum']) > 0) {
// get routing number
$routeNum = $_POST['routingNum'];

// check for numeric
if (is_numeric($routeNum)) {

// check length
if (strlen($routeNum) == 9) {
// call validate_routing_number()
$routeNumValidated = FALSE; // assume not validated
validate_routing_number($routeNum);

// see if routing number is validated
if ($routeNumValidated) {
$msg = $msg . "<p>Routing number validated.</p>";
} else {
$msg = $msg . "<p>Invalid routing number submitted.</p>";
}
} else {
$routeNum = FALSE;
$msg = $msg . "<p>Routing Number must be 9 digits.</p>";
}

} else { // routing number is not numeric
$routeNum = FALSE;
$msg = $msg . "<p>Routing number must contain only numbers.</p>";
}

} else {
$msg = $msg . "<p>You must enter a routing number.</p>";
}

// print message if there is one
if (isset($msg)) {
echo '<div align="center" style="color:red; font-weight:bold">' . $msg . '</div>';
}

} // end of submit conditional

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset><legend><b>Enter your ABA Routing Number below: </b></legend>
<input type="text" name="routingNum" size="12" maxlength="12" />
</fieldset>

<div align="center"><input type="submit" name="submit" value="Send" /></div>

</form>

<?php
include('includes/footer.html');
?>[/code:1]

What happens is that even if the variable submitted passes the algorithm check in the function, my calling script still prints "Invalid routing number submitted." Shouldn't the return variable $routeNumValidated be TRUE (I've also tried setting it to 1 in the function), cause the conditional following the function call to print "Routing number validated."? I can't figure out what I'm doing wrong.

Any suggestions gratefully accepted!

Paul

Problem solved!

Nevermind! :roll:

I figured out that I needed to make $routeNumValidated global in the function. Now it works as expected.

Thanks for reading!

Paul

Re:Returning values from a function

Better yet, you can just assign the return value from the function to a variable like this:

$routeNumValidated = validate_routing_number($routeNum);

That way, you avoid using a global variable inside your function. It is sometimes said that relying on global variables is bad.

trouble with above

e

Re:Returning values from a function

Thanks, Moxley.

That makes perfect sense. I don't know why that approach didn't occur to me, since I've been doing exactly that with other functions such as mysql_query().

Paul

Adelhard

comprare viagra
compro viagra
buy viagra no prescription
phentermine 90
vente viagra
viagra uk
viagra over the counter
vardenafil from india
viagra for sale
viagra softtabs
vente cialis
viagra soft tablets
buy phentermine without doctor
phentermine 30mg no rx
acheter tadalafil
buy viagra without a prescription
viagra senza ricetta
viagra capsules
vardenafil 10 mg
achat viagra
vardenafil hcl 10mg
free viagra
vardenafil tablets
acquista levitra
viagra from india
phentermine by cod
buy viagra
pharmacy selling phentermine
viagra without rx
commande viagra
commande kamagra
viagra cialis levitra comparison
compro levitra
compra cialis
achat sildenafil
over the counter vardenafil
phentermine diet tablets
viagra canadian pharmacy
buy phentermine online us
phentermine cost
otc viagra
cheapest viagra
viagra no prescription needed
vardenafil 20mg
vendo cialis
indian viagra
levitra senza ricetta
levitra en ligne
order phentermine online consultation
blue viagra
vendita cialis
viagra canada
phentermine online no script
phentermine blue white
levitra generique
cheap vardenafil
cheap phentermine online
cialis generico
liquid viagra
vente levitra
levitra sans ordonnance
order viagra
phentermine 37.5 cod
vardenafil uk
viagra sans ordonnance
cost of phentermine
vendo viagra
viagra drug
phentermine 37.5 tablets
online doctor for phentermine
viagra 50mg
viagra non prescription
viagra overnight delivery
viagra en ligne
viagra tabs
viagra by mail
acheter kamagra
buy phentermine without doctor approval
achater cialis
phentermine no prescription needed
viagra 100mg
phentermine 30 mg no rx
achat levitra
viagra prescription
cialis senza ricetta
buy phentermine 15
achat cialis
viagra 100 mg
cheap phentermine online consultation
phentermine from online
vardenafil 20 mg
generic viagra
phentermine delivered cod
phentermine hcl 37.5 mg
viagra pills
phentermine no prescription 30
phentermine online no prescription
buy phentermine online no rx
buy phentermine yellow
vente kamagra
buy vardenafil
viagra low cost
acheter viagra
phentermine w out prescription
cheap viagra
get phentermine without prescription
compra viagra
compro cialis
levitra generico
phentermine blue clear
viagra generique
vardenafil online
achater levitra
vardenafil india
phentermine for cheap
purchase viagra
vendo levitra
site to order phentermine
commander cialis
viagra soft tabs
phentermine no rx needed
viagra australia
phentermine no prescription 90
viagra price
buy phentermine online no perscription
phentermine blue & clear
viagra cost
phentermine blue and white capsules
phentermine cod
commande cialis
pharmacy order phentermine
acquisto cialis
viagra supplies
comprare cialis
viagra mail order
cialis sans ordonnance
viagra discount
achat kamagra
phentermine 37.5 mg no rx