tags:

views:

127

answers:

11

Hi,

I'm working on modifying some existing code for a payment gateway and I'm not sure what this means. Is it saying something like the 4th element of the array coming back from the gateway? If someone can tell me what it means it would be a big help.

$transactionID = (isset($authNetCodes[4])) ? $authNetCodes[4] : 0;
$transactionMessage = (isset($authNetCodes[3])) ? $authNetCodes[3] : "";

Any help would be appreciated.

Thanks, JK

+2  A: 

If the 5th element of the authNetCodes array is set, set that value to $transactionID otherwise set it to 0.

Same story with $transactionMessage except that it will be set to a zero length string.

The <boolean expression> ? <if true> : <if false> construct is just a shorthand for a simple if-else structure.

Spencer Ruport
And thanks for the reminder that it's actually the 5th element.Wasn't that a movie?Thanks for the help!
yup - Bruce Willis and Milla Jovovich. Good movie.
Spencer Ruport
+1  A: 

The code is using short form of if/else: (condition ? if true : if false)

In this case, it's setting $transactionID to $authNetCodes[4] if it exists, or 0 if it doesn't. It's setting $transactionMessage to $authNetCodes[3] if it exists, or an empty string if it doesn't.

ceejayoz
+7  A: 

That's a pair of ternary operations.

$transactionID = (isset($authNetCodes[4])) ? $authNetCodes[4] : 0; means:

IF $authNetCodes[4] has a value (isset) THEN $transactionID = $authNetCodes[4] ELSE $transactionID = 0.

$transactionMessage = (isset($authNetCodes[3])) ? $authNetCodes[3] : ""; means:

IF $authNetCodes[3] has a value (isset) THEN $transactionMessage = $authNetCodes[3] ELSE $transactionID = "" (empty string).

Nathan Taylor
Thanks. I'm sure others have the same thing or similar but this one is the clearest to me anyway. Well done. And my thanks!
Glad I could help!
Nathan Taylor
+1  A: 

it just an assign statement

if $authNetCodes[4] has a value set that value to $transactionID else set $transactionID equal to 0.

its the same for the other statement.

Jon
+1  A: 

if $authNetCodes[4] is set ( not null ) then $transactionID = $authNetCodes[4] else $transactionID = 0

if $authNetCodes[3] is set ( not null ) then $transactionMessage = $authNetCodes[3] else $transactionMessage = "" //Empty string

Nettogrof
+1  A: 

This

$transactionID = (isset($authNetCodes[4])) ? $authNetCodes[4] : 0;

Can be read as

if (isset($authNetCodes[4])) 
{
    $transactionID = $authNetCodes[4];
}
else
{
    $transactionID = 0;
}

Similarly, this

$transactionMessage = (isset($authNetCodes[3])) ? $authNetCodes[3] : "";

can be read as

if((isset($authNetCodes[3])))
{
    $transactionMessage = $authNetCodes[3];
}
else
{
    $transactionMessage = "";
}

In plain english, both code fragments are saying "if this array element is set, assign the value of that array element to this variable, otherwise assign some other values (0 and and empty string). The ? : construct is called a ternary operator.

Alan Storm
Your first code block else statement should be: $transactionID = 0;
Jeff
Thanks, that should be fixed!
Alan Storm
+1  A: 
$transactionID = (isset($authNetCodes[4])) ? $authNetCodes[4] : 0;

Set transactionID to the 5th element of authNetCodes if it is set, otherwise 0

$transactionMessage = (isset($authNetCodes[3])) ? $authNetCodes[3] : "";

Set $transactionMessage to the 4th element of authNetCodes if it is set, otherwise empty string.

JYelton
+1  A: 

First:

$transactionID = (isset($authNetCodes[4])) ? $authNetCodes[4] : 0;

This means that, if the fifth element of $authNetCodes (remember, arrays are zero-indexed!) has a value, then set $transactionID to that value. Otherwise, set $transactionID to 0.

Second:

$transactionMessage = (isset($authNetCodes[3])) ? $authNetCodes[3] : "";

Likewise, if the fourth element of authNetCodes has a value, set $transactionMessage to that value. Otherwise, set $transactionMessage to an empty string.

As far as where $authNetCodes comes from, this code doesn't say.

Lucas Oman
+1  A: 

This code can be re-written this way :

if (isset($authNetCodes[4])) {
    $transactionID = $authNetCodes[4];
} else {
    $transactionID = 0;
}

if (isset($authNetCodes[3]) {
    $transactionMessage = $authNetCodes[3];
} else {
    $transactionMessage = "";
}

Which means :

  • if there is a value at index = 4 in the $authNetCodes array, then put this value in $transactionID
  • else, put 0 in $transactionID

And, for the second condition :

  • if there is a value at index = 3 in $authNetCodes, then put its value in $transactionMessage
  • else, put an empty string in $transactionMessage


In PHP, the "?:" is the Ternary Operator

Pascal MARTIN
+1  A: 

$transactionID value is: (If $authNetCodes[4] has value, give its value, otherwise 0.) $transactionMessage is: (If $authNetCodes[3] has value, give its value, otherwise "".)

waqasahmed
+1  A: 

The operator <condition expr> ? <true expr> : <false expr> is the conditional operator. If <condition expr> is true, <true expr> is evaluated, <false expr> otherwise.

Your statement

$transactionID = (isset($authNetCodes[4])) ? $authNetCodes[4] : 0;

is the same as

if (isset($authNetCodes[4])) {
    $transactionID = $authNetCodes[4];
} else {
    $transactionID = 0;
}
Gumbo