Jump to content

Recommended Posts

Working with authorize.net script that is included in my php.  Trying to figure out how to use a variable beyond the 'case' I can do just about anything... inside the case... But I need to use the variable outside of there.

 

//several cases above
case 5:
					$this->approval_code = $pstr_trimmed;
					break;

// case 7 is the authorize.net transaction ID - This is the one I want to work with
case 7:
					echo "Transaction ID: ";
					echo $pstr_trimmed;
					$trans_id_test = $pstr_trimmed;

					break;
// but this below won't work. I can't use the variable anywhere outside of the case.
				echo "the new variable is now...".$trans_id_test;

Link to comment
https://forums.phpfreaks.com/topic/259500-variable-wont-exist-beyond-break/
Share on other sites

$myVar = 1;
switch($myVar){

case 1:
$this->anotherVar = 'Its one';
break;

case 2:
$this->anotherVar = 'Its two';
break;
}

echo $this->anotherVar;
// outputs its one

 

Basically, with the BREAK

If the statement in the case is TRUE, then the break will come out of the SWITCH

 

You can use

$myVar = 1;
switch($myVar){
case 1:
$this->anotherVar ='its One';
case 2:
$this->anotherVar = 'itsTwo';

case < 5:
$this->anotherVar ' Its Less than 5';
}

echo $this->anotherVar;
// result would be Its Less than 5;

 

Because you did not break out of any switch, and the last statement is true but overwrites the first value;

Hmmm... I'm new with cases, but I think I've already tried this. Maybe I left out a key point. This is in an included php file. I need to get it from the included php file where this case is, and use it in the parent php file. I can include the php files but they're a bit long. But here's the case, trimmed down here from file AIM.class.php. Below it is the page that I include this file on...here, I'm trying to echo on the last few lines of that code.

 

 
$trans_id_test = '';
switch($j) {
// case 1 is the general approved/declined/error result
	case 1:
					if ($pstr_trimmed=="1")
						$this->status = "Approved";
					elseif ($pstr_trimmed=="2") {
						$this->status = "Declined";
						//$this->errorStack[] = "The card was declined.";
					}elseif ($pstr_trimmed=="3") {
						$this->status = "Error";
						//$this->errorStack[] = "An error occurred.";
					}
					break;

				// case 2 is the sub code

				// case 3 is the response code

				// case 4 is the detailed response text (readable)

				// case 5 is the authorize.net approval code

				// case 7 is the authorize.net transaction ID

				case 7:
					echo "Transaction ID: ";
					echo $pstr_trimmed;
					$trans_id_test = $pstr_trimmed;
					// I've set a separate variable here, because $pstr_trimmed gets reused in each case.
					break;

				// case 38 is the MD5 to prove it came from authorize.net

				// case 39 is the code result

				// the rest of the messages are just stored for debugging purposes (or random use)
				default:
					$this->remaining[$j] = $pstr_trimmed;
					break;
			} // here I can echo the variable I set, but it doesn't get to the parent page code, below this sample.
echo "the new variable is now...".$trans_id_test;

 

And here's the page where the above is included...

 


<?php


  
require("AIM2.class.php");
$aim = new AIM("asdfasf", "adfasdf");


// If you want to send test transactions, use the setTesting method to
// tell the class to talk to the certification server
//$aim->setTesting(1);
$expfull = $_REQUEST['expmonth'] . $_REQUEST['expyear'];
$nameoncard = $_REQUEST['cc_name'];
$final_total = $_REQUEST['final_total'];
if (!$aim->setCard($nameoncard, $_REQUEST['ccnumber'], $_REQUEST['expmonth'], $_REQUEST['expyear'], $_REQUEST['cc_code']))
die("The credit card is invalid: " . print_r($aim->errorStack, 1));

// Sets the customer's billing information
$aim->setBuyer($_REQUEST['fname'], $_REQUEST['lname'], $_REQUEST['address'], $_REQUEST['city'], $_REQUEST['state'], $_REQUEST['zip'], $_REQUEST['email'], $_REQUEST['phone']);

// Attempt to process the card and set $approval to the approval code
if (!$approval = $aim->processCard("company name", $final_total)){
echo "<br><strong><h3><font color=#F63026> Oops!</font></h3></strong><br> There was an error proccessing the credit card! <br> Please <a href='registration.php'>return to the registration page</a> to try again, or <br>contact us to register over the phone at <b>480-245-9724</b><br> <br>" . print_r($aim->errorStack,1);
$appcodedesc = "Error processing credit card.";
$approval = 0;
}else{
// If we made it this far, the card was successfully charged
echo "<strong><h3>Payment Approved</h3></strong><br>Successfully charged the credit card.  You have completed your registration On the next page, you can print for your records. Please note that these are secure pages for payment processing, and pressing the back button in your browser, will bring up an expired page<br><br>"; // Approval code: " . $approval;

// here is what I cannot get to echo.
echo $trans_id_test;

$approval = 1;
$appcodedesc = "Payment Approved";

Is this switch within a class? If so, you are running into a scope issue. You'll have to mark the variable as Global within the class.

http://php.net/manual/en/language.variables.scope.php

 

globals should never be used.

They can cause variable pollution and completely break the encapsulation of a class/function.

If you need access to a variable in the global scope, pass it through the argument list.

I still do not understand the issue.

The issue is obviously that I'm very new to Classes and cases. :)

 

I have a registration form that runs a transaction to authorize.net, using the code in the AIM.class.php that is included in registrationpage.php

 

Case 7, if I understand right, is the transaction id that is returned from authorize.net

 

I need to put this id into a database, but can't do it within the case. but that set variable doesn't exist outside of the case. ... and apparently outside of the class.

 

I do agree with AyKay47, you really shouldn't use globals. The reason classes have scope in the first place is to avoid cross-contamination with other code. I have dealt with cases, however, where using a global saved hours of reprogramming. Just use them with extreme caution.

 

Your variable has no problem leaving the case, but is trapped within the scope of the class.

 

AyKay47, the issue turpentyne is having is changing the value of a variable within a class, and not seeing any changes in the variable outside the class. The issue is thought to be because of using switch/case statements, but the real issue is a scope issue within a class. I was able to guess that classes were being used because of the '$this->' pointers.

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.