PHP Single vs Double Question Mark: Difference & Code Examples
Difference between single vs double question mark in PHP The single question mark is used for a ternary operator for simple if / then statements, whereas the double is used for null coalescing. The double question mark will return the first non-null value. You can run multiple instances of the double question mark in a single statement if there's a chance you have multiple null values. Now, let's discuss PHP single double question mark starting from the code example. Single vs Double Question Mark PHP Code Example // /// Single Question Mark Ternary Example // $iLikeCats = false; echo $iLikeCats ? 'Yes, cats are awesome!' : 'No way, I can't stand cats!'; // output: 'No way, I can't stand cats!'; /// Double Question Mark Null Coalescing Example // $userName = null; $defaultName = 'Guest'; $displayName = $userName ?? $defaultName; echo $displayName; // Output: Guest This article aims to provide a…