Jump to content


Photo

Recieving variables from a function


  • Please log in to reply
8 replies to this topic

#1 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 October 2006 - 07:40 AM

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



#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 October 2006 - 07:53 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 October 2006 - 08:04 AM

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

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 October 2006 - 08:06 AM

yeah sure, you could do that if you want. dunno if that works for you logically, but syntatically that's correct.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 October 2006 - 08:16 AM

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

#6 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 01 October 2006 - 09:52 AM

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.
Good luck with your coding.
Jason / ProjectFear / Jaysonic

#7 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 October 2006 - 11:51 AM

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.

#8 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 October 2006 - 12:44 PM

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.

#9 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 03 October 2006 - 12:19 PM

oh okie dokie. far enuf then. so this has been solved...
Good luck with your coding.
Jason / ProjectFear / Jaysonic




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users