This question already has an answer here:
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 ?
</div>
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.
If Masked == True Then $frameHeadHead[1] = $payloadLength + 128
Else $frameHeadHead[1] = $payloadLength
Its ternary operator.
$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
Means:
If Masked === True
Then $frameHeadHead[1] = $payloadLength + 128
Else
Then $frameHeadHead[1] = $payloadLength
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;
}
$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
it conditional statement like if and else
if($masked === true){ $payloadLength + 128 } else {$payloadLength;}
Check below:
==
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
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;
}