rfc:isset_ternary
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:isset_ternary [2014/09/06 00:15] – ajf | rfc:isset_ternary [2015/12/24 21:13] – Correct spelling of 'practice' ajf | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Implicit isset() in Shorthand Ternary | + | ====== PHP RFC: Null Coalesce |
- | * Version: 0.1 | + | * Version: 0.2.3 |
- | * Date: 2014-09-06 | + | * Date: 2014-09-06, last updated 2014-09-16 |
- | * Author: Andrea Faulds, ajf@ajf.me | + | * Author: Andrea Faulds |
- | * Status: | + | * Contributor: |
+ | * Status: | ||
* First Published at: http:// | * First Published at: http:// | ||
Line 9: | Line 10: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | PHP is a web-focussed programming language, so processing user data is a frequent activity. In such processing it is common to check for something' | + | PHP is a web-focussed programming language, so processing user data is a frequent activity. In such processing it is common to check for something' |
===== Proposal ===== | ===== Proposal ===== | ||
- | The ''? | + | The coalesce, or ''? |
- | Internally this is actually implemented by rewriting | + | <code php> |
+ | // Fetches | ||
+ | $username = $_GET['user'] ?? 'nobody'; | ||
+ | // equivalent to: $username = isset($_GET[' | ||
- | ==== Backwards-compatibility considerations | + | // Calls a hypothetical model-getting function, and uses the provided default if it fails |
+ | $model | ||
+ | // equivalent to: if (($model | ||
- | Because '' | + | // Parse JSON image metadata, and if the width is missing, assume 100 |
+ | $imageData = json_decode(file_get_contents('php://input')); | ||
+ | $width = $imageData['width' | ||
+ | // equivalent | ||
+ | </ | ||
- | ===== Proposed PHP Version(s) ===== | + | It can even be chained: |
- | This proposed for the next PHP x, which at the time of this writing would be PHP 7. | + | <code php> |
+ | $x = NULL; | ||
+ | $y = NULL; | ||
+ | $z = 3; | ||
+ | var_dump($x ?? $y ?? $z); // int(3) | ||
- | ===== Open Issues ===== | + | $x = [" |
+ | var_dump($x[" | ||
+ | </ | ||
- | None I'm aware of. | + | This example demonstrates the precedence relative to the ternary operator and the boolean or operator, which is the same as C#: |
- | ===== Unaffected PHP Functionality ===== | + | <code php> |
+ | var_dump(2 ?? 3 ? 4 : 5); // (2 ?? 3) ? 4 : 5 | ||
+ | var_dump(0 || 2 ?? 3 ? 4 : 5); // ((0 || 2) ?? 3) ? 4 : 5 => int(4) | ||
+ | </ | ||
- | The behaviour of the long-form ternary | + | This example demonstrates how it is a short-circuiting |
- | ===== Future Scope ===== | + | <code php> |
+ | function foo() { | ||
+ | echo " | ||
+ | } | ||
+ | var_dump(true ?? foo()); // outputs bool(true), " | ||
+ | </ | ||
- | None I can think of currently. | + | ===== Proposed PHP Version(s) ===== |
- | ===== Proposed Voting Choices ===== | + | This proposed for the next PHP x, which at the time of this writing |
- | + | ||
- | As this is a language change, a 2/3 majority is required. A straight Yes/No vote would be held. | + | |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | A pull request with a working implementation and test, targeting master, is here: https:// | + | A pull request with a working implementation and test, targeting master, is here: https:// |
+ | |||
+ | The original patch was graciously provided by Nikita Popov. | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | As this is a language change, a 2/3 majority is required. A straight Yes/No vote is being held. | ||
+ | |||
+ | Voting started on 2014-09-20 and ended on 2014-09-27. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Implementation ===== | ===== Implementation ===== | ||
- | After the project is implemented, | + | |
- | - the version(s) it was merged to | + | Merged into master |
- | | + | |
- | - a link to the PHP manual entry for the feature | + | Not yet documented. |
===== References ===== | ===== References ===== | ||
Line 59: | Line 94: | ||
* 2006: http:// | * 2006: http:// | ||
* 2007: http:// | * 2007: http:// | ||
- | * 2008: [[rfc: | + | * 2008: [[rfc: |
* 2009: http:// | * 2009: http:// | ||
* 2011: http:// | * 2011: http:// | ||
Line 65: | Line 100: | ||
This list is quite probably incomplete. | This list is quite probably incomplete. | ||
+ | |||
+ | Operator precedence in C#: http:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Keep this updated with features that were discussed on the mail lists. | Keep this updated with features that were discussed on the mail lists. | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * v0.2.3 - Added short-circuit example | ||
+ | * v0.2.2 - Added precedence example | ||
+ | * v0.2.1 - Added chaining example | ||
+ | * v0.2 - Overhauled proposal, proposing new operator ''??'' | ||
+ | * v0.1 - Created |
rfc/isset_ternary.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1