Jump to content

Archived

This topic is now archived and is closed to further replies.

fezzik

Recieving variables from a function

Recommended Posts

I'm new to PHP. I'm currenty exploring the use of functions. I found a function that validates credit card numbers and was wondering if I could get some help with how to return variables from the function. Here is the function and code I use:

function checkCreditCard ($cardnumber, $cardname, &$errornumber, &$errortext) {

// validate the cardnumber, cardname...

// if the card does not pass validation return false.
// the following is an example of a if statement that checks string length

$ccErrors [1] = "No card number provided";

if (strlen($cardnumber) == 0)  {
  $errornumber = 1;   
  $errortext = $ccErrors [$errornumber];
  return false;
  }
}

if ( !empty( $customer_CardNumber ) ) {
  if (checkCreditCard ($customer_CardNumber, $_SESSION['customer_paymentMethod'], $ccerror, $ccerrortext)) {
    $ccerrortext = 'This card has a valid format';
  } else {
    // display $errortext and $errornumber
    }
  }

I noticed that the variables &$errornumber and &$errortext contain a "&" which I have not seen before. From reading a little it looks like this variable is assigned by reference. I'm not sure that this affects what I would like to accomplish but I would like to pass the variables $errornumber and $errortext back to the page. Any suggestions on how to do this? I believe the script should appear where I have "// display $errortext and $errornumber" Any help is much appreciated.

Best,

Fezzik

Share this post


Link to post
Share on other sites
normally when you pass a variable as an argument in a function, the function creates a local copy of that variable. Altering the variable inside the function will not change the original variable. when a variable is passed by reference, and you alter the variable inside the function, it affects the variable not only inside the function, but from the scope if came from.

As far as passing the variables back to the page, a function can only return one thing.  So you are going to have to create an array that holds both variable values and pass the array back, if that's what you want to do.  If you are simply wanting to echo the variables, then you might consider simply echoing them inside that function instead of passing them back somewhere else.  I suppose it really depends on how you have everything setup, as to which would be the best method for you.

Share this post


Link to post
Share on other sites
Thanks for the response. If I wanted to pass $errortext to the page would this be the correct method:

if (strlen($cardnumber) == 0)  {
  $errornumber = 1;   
  $errortext = $ccErrors [$errornumber];
  return ($errortext);
  }
}

Cheers,

Fezzik

Share this post


Link to post
Share on other sites
yeah sure, you could do that if you want. dunno if that works for you [i]logically[/i], but syntatically that's correct.

Share this post


Link to post
Share on other sites
I gave this method a try and found that I'm not able to recieve the value of this variable. I'm sure my scripting is wrong but here is the delaration I tried to recieve the value:

$ccString = checkCreditCard ($customer_CardNumber, $_SESSION['customer_paymentMethod'], $ccerror, $ccerrortext);

When I pass an invalid CC number thru the function it recognizes it as invalid but does not assign a value to $ccString. Is the method I use to recieve this value incorrect? If so, can you please show me an example of how to assign the value of $errortext to a variable outside the function.

Thanks,

Fezzik

Share this post


Link to post
Share on other sites
the method is correct. i am pretty sure. so ur 100% sure its not assigning the error text to $ccString. did u try printing it. thats odd. i'll have a looky over.

Share this post


Link to post
Share on other sites
Thanks for responding Crayon! Yes, I'm 100% sure that it is not assigning $errortext to $ccString. I do print $ccString. I have another similiar function which I use the same method and it works but unfortunately with this function it does not. I appreciate any help or feedback.

Share this post


Link to post
Share on other sites
In case if anyone was interested I finally figured this out. The function assigns &$errornumber and &$errortext by reference:

function checkCreditCard ($cardnumber, $cardname, &$errornumber, &$errortext) {}

The below validation is found in the function. It checks to see if the string is null:

// Ensure that the user has provided a credit card number
  if (strlen($cardnumber) == 0)  {
    $errornumber = 1;   
    $errortext = $ccErrors [$errornumber];
    return false;
  }

So, when we call the function:

if (checkCreditCard ($customer_CardNumber, $_SESSION['customer_paymentMethod'], $ccerror, $ccerrortext)) {
  $ccerrortext = 'This card has a valid format';
}

If the string, $customer_CardNumber, is null the function assigns a value to $errornumber and $errortext and returns false. Since both $errornumber and $errortext are assigned by reference it replaces the value of  $ccerrortext in the call script. You can then print $ccerrortext.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.