conventions:php_null
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
php_null [2021/08/17 16:02] – created diogo | conventions:php_null [2021/12/08 00:28] (current) – ↷ Page moved from php_null to conventions:php_null diogo | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== The absence of value ====== | ====== The absence of value ====== | ||
+ | |||
< | < | ||
- | When programming in PHP it's common | + | When programming in PHP it's common |
A variable that wasn't initialized yet or a function that could not produce a value. | A variable that wasn't initialized yet or a function that could not produce a value. | ||
On the latter, one could be tempted to throw an exception in these scenarios, but not always that kind | On the latter, one could be tempted to throw an exception in these scenarios, but not always that kind | ||
Line 25: | Line 26: | ||
===== We've put to vote two options ===== | ===== We've put to vote two options ===== | ||
- | ==== Option 1: Use === null ==== | + | ==== Option 1: Use === null (most of the time) ==== |
< | < | ||
- | On the discussion of whether to **use `=== null` vs [`is_null()`](https:// | + | Use one, or the other in function of context. This is better illustrated with two example situations: |
- | the literature online is diverse and divided. | + | |
- | + | ||
- | Some facts to consider: | + | |
- | * [null is both a data type, and a value](https:// | + | |
- | * As noted in PHP's documentation, | + | |
- | * A variable with null value returns false in an [isset()](https:// | + | |
- | despite that, assigning a variable to NULL is _not_ the same as [unsetting](https:// | + | |
- | To actually test whether a variable is set or not [requires adopting different strategies per context](https:// | + | |
- | * The [void return type](https:// | + | |
- | an expression, it evaluates to null. | + | |
- | + | ||
- | Therefore, in GNU social we would expect you to use one, or the other in function of context. | + | |
- | This is better illustrated with two example situations: | + | |
* If you're testing if a function returned null, then you're not testing a variable' | * If you're testing if a function returned null, then you're not testing a variable' | ||
Line 49: | Line 37: | ||
would with a `is_int($var)` | would with a `is_int($var)` | ||
| | ||
- | About [nullable types](https:// | ||
- | we prefer that you _use_ the shorthand `?T` instead of the full form `T|null` as it suggests that you're considering the | ||
- | possibility of not having the value of a certain variable. This is reinforced by the fact that NULL can not be a | ||
- | standalone type in PHP. | ||
</ | </ | ||
==== Option 2: Use is_null() ==== | ==== Option 2: Use is_null() ==== | ||
Line 75: | Line 59: | ||
A non-void function, by definition, is expected to return a value. | A non-void function, by definition, is expected to return a value. | ||
If it couldn' | If it couldn' | ||
- | strict comparison. Hence, as you're testing whether a variable is of type null, then you should use `is_null($var)`. | + | strict comparison. Hence, as you're testing whether a variable |
Just as you normally would with an `is_int($var)` | Just as you normally would with an `is_int($var)` | ||
</ | </ | ||
Line 82: | Line 66: | ||
< | < | ||
- | Voter|is_null|=== null | + | Voter | `is_null()` | `=== null` |
- | -----|-------|-------- | + | ---------|-------------|--------- |
+ | XRevan86 |X |- | ||
+ | Diogo |X |- | ||
+ | Hugo | ||
+ | Eliseu | ||
</ | </ |
conventions/php_null.1629216147.txt.gz · Last modified: 2021/08/17 16:02 by diogo