Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ?int $number and int $number = null?

Tags:

php

Given this explanation

Nullable types: Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.

https://www.php.net/manual/en/migration71.new-features.php

The following code :

 public function test(?int $var) {  } 

Means that test() can be called with $var either as int or as null.

And the following code :

 public function test(int $var = null) {  } 

Means that test() can be called with $var either as int or as null as well.

What are the differences between those two methods ? Is any of these more performant than the other?

like image 885
tomsihap Avatar asked Aug 29 '19 08:08

tomsihap


People also ask

What does int null mean?

Value types like int contain direct values rather than references like ReferenceType . A value cannot be null but a reference can be null which means it is pointing to nothing. A value cannot have nothing in it.

Can null be compared to int?

As seen in the above example, int cannot be null. On the other hand, Integer is an object which can be checked for the null property.

What is difference between int and int in C#?

Answers. int is C#'s alias for the System. Int32 datatype, and represents a 32-bit signed integer. int?, on the other hand, is the shortcut way of saying Nullable<int>.

Can null be compared to Integer in Java?

Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.


2 Answers

It is important to distinguish between the two language features being discussed here, that is, type declarations and default argument values.

The first function is only using type declarations, this means that the input argument has to be of the type int or NULL.

The second function is using both type declarations and default argument values, this means that the argument has to be of the type int or NULL but if omitted it will default to NULL.

Take your first function, if you simply called test() without passing anything to it, you'd get:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function test() [...]

which is correct as the function expects either int or NULL but got neither whereas for the second, as you have defined the argument with a default value, it would run without errors.

Code

function test(?int $var) {   var_dump($var); }  function test2(int $var = null) {   var_dump($var); }  test(1); // fine test(); // error test2(1); // fine test2(); // fine 

As far as performance goes, the difference is probably negligible, nothing significant enough that would be a cause for concern.

Live Example

Repl

like image 164
Script47 Avatar answered Oct 09 '22 06:10

Script47


If the language were designed today, int $var = null would probably be an error, and should really be written ?int $var = null. The two parts mean different things:

  • The ? indicates that null is a valid value for that parameter.
  • The = null indicates that null is the default if the parameter is not passed.

However, before the ?type syntax was introduced, there was a special case in the language: if null is given as the default for a parameter, then it is legal to pass null to that parameter, even if the type declaration would otherwise prevent it.

like image 20
IMSoP Avatar answered Oct 09 '22 05:10

IMSoP