September 2017
Intermediate to advanced
244 pages
6h 44m
English
If we are type hinting data types of parameters or return types of function, then it is important that there should be a way to pass or return the NULL data type instead of type mentioning as parameter or return type. There can be different scenarios when we may need this, but always what we need to do is place a ? before the data type. Let's say we want to type hint string; if we want to make it nullable, that is to allow NULL as well, we will just type hint it as the ? string.
For example:
<?phpfunction testReturn(): ?string{ return 'testing';}var_dump(testReturn());// string(10) "testing"function testReturn2(): ?string{ return null;}var_dump(testReturn2());//NULLfunction test(?string $name){ var_dump($name);}test('testing'); ...Read now
Unlock full access