1

This is an extract from a WebSocket client, what does the following code line mean/do ?

$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;

I read it this way (check below)

If Masked == True Then $frameHeadHead[1] = $payloadLength + 128 / $payloadLength

I don't understand the ($masked === true) as well as I dont understand the : $payLoadLength; (what is the : symbol for ?)

And what if Masked == False ? There is no result ?

Jack M.
  • 3,676
  • 5
  • 25
  • 36

6 Answers6

7

That(?:) is called the ternary operator.

(condition) ? /* if condition is true then return this value */ 
            : /* if condition is false then return this value */ ;

Also === compares the internal object id of the objects. It is used for strict comparison. "===" means that they are identical.

On a side note:

Note: Please note that the ternary operator is an expression, and that it doesn't evaluate to a variable, but to the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
5
$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;

it  conditional statement like if and else  

 if($masked === true){ $payloadLength + 128 } else {$payloadLength;}
karan
  • 164
  • 10
3

first: the === checks if both the value and the type equal (so while false==0 is true, false===0 isn't). the reverse would be !==.

the var=bool ? value1 : value2 statement is the same as:

if(bool){
  var=value1;
}else{
  var=value2;
}

so your line translates to:

if($masked===true){
  $frameHead[1] = $payloadLength + 128;
}else{
  $frameHead[1] = $payloadLength;
}
nonchip
  • 1,084
  • 1
  • 18
  • 36
2
If Masked == True Then $frameHeadHead[1] = $payloadLength + 128 
Else $frameHeadHead[1] = $payloadLength
2

Its ternary operator.

$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;

Means:

If Masked === True 

Then $frameHeadHead[1] = $payloadLength + 128 

Else

Then $frameHeadHead[1] = $payloadLength
Manwal
  • 23,450
  • 12
  • 63
  • 93
2

Check below:

Why we use === instead of == ?

== is used for matching the value. === is used for matching the value with datatype.

More precisely, lets check one example -

99 == "99" // true
99 === "99" // false

(?:) mean

This is called a ternary operator. It means

$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;

if ($masked === true) {
    $frameHead[1] = $payloadLength + 128;
} else {
   $frameHead[1] = $payloadLength;
}
prava
  • 3,916
  • 2
  • 24
  • 35