fezzik Posted October 1, 2006 Share Posted October 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/ Share on other sites More sharing options...
.josh Posted October 1, 2006 Share Posted October 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-101715 Share on other sites More sharing options...
fezzik Posted October 1, 2006 Author Share Posted October 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-101716 Share on other sites More sharing options...
.josh Posted October 1, 2006 Share Posted October 1, 2006 yeah sure, you could do that if you want. dunno if that works for you [i]logically[/i], but syntatically that's correct. Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-101717 Share on other sites More sharing options...
fezzik Posted October 1, 2006 Author Share Posted October 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-101719 Share on other sites More sharing options...
JasonLewis Posted October 1, 2006 Share Posted October 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-101763 Share on other sites More sharing options...
fezzik Posted October 1, 2006 Author Share Posted October 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-101779 Share on other sites More sharing options...
fezzik Posted October 1, 2006 Author Share Posted October 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-101786 Share on other sites More sharing options...
JasonLewis Posted October 3, 2006 Share Posted October 3, 2006 oh okie dokie. far enuf then. so this has been solved... Quote Link to comment https://forums.phpfreaks.com/topic/22644-recieving-variables-from-a-function/#findComment-102991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.