Difference between "?:" and "??" operator in PHP
?? operator
- Never cause to undefined warning error
- If on left side value is null or undefined returns right side of operator, in other cases returns value on left side.
- echo null ?? "right" ; // right
- echo $adsaada ?? "right"; //right
- echo "" ?? "right"; // ""
- echo 0 ?? "right"; // 0
- echo true ?? false; // 1
"?:" operator -actually this is a bit more shorthand of ternary operator. Checks whether result is true or not. Also returns undefined warning error if not exists
- echo null ?? "right"; // right
- echo $not_exists_var ?? // undefined warning error then "right";
- echo "" ?? "right"; // ""
- echo 0 ?? "right"; // "right"
- echo true ?? false; 1
Comments
Post a Comment