Jump to content

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

Link to comment
https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/
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.
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
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.
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.