-1

I read this article and was confused about this code public ?string $b = 'foo';

As far as I understand $b is a string variable. Is the ? before string intended to mention that this variable is also nullable? If so than it is the same as public string | null $b = 'foo'; which is in my option much intutive to understand.

1 Answers1

2

Since PHP 7.1 nullable return types or nullable type hinting with a question mark exists. Since PHP 8.0 union types are available. If you want to write null|string you can do this since PHP 8.0. This notation expresses the same as ?string.

Which notation you use is up to your personal taste. Since the notation with the question mark is shorter, I personally would prefer this notation.

Marcel
  • 4,854
  • 1
  • 14
  • 24