-1

In php, what is the difference between the two following variable declarations, both for procedural mode and inside a class:

  • string $str; // without ? before the type
  • ?string $str; // with ? before the type

Thank you for your lights

Hamid ER-REMLI
  • 200
  • 1
  • 7
  • 5
    Does this answer your question? [Reference — What does this symbol mean in PHP?](https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – El_Vanja May 19 '21 at 10:10
  • It indicates that the parameter can be null. – El_Vanja May 19 '21 at 10:10
  • Thank you. I've seen. the point in the long list talks mostly about nullable functions, wich means a function can return a null, but have found about a variable declaration – Hamid ER-REMLI May 19 '21 at 10:26
  • There's a link to a specific topic about this in there: https://stackoverflow.com/questions/48881489/php-method-argument-type-hinting-with-question-mark-type The same applies to class properties, it's no different. – El_Vanja May 19 '21 at 10:27
  • I saw the link. Yes, the question mark can make a sens when used with functions, either with paramaters or with return values. My question concerns only a pure variable in procedural mode or attribute in a class! most responses i saw say that when the type is preceded by ? this means that the variable may have a null value! But, even if I don't precede it with ? I can assign null to it !!! – Hamid ER-REMLI May 19 '21 at 11:15
  • Type hints cannot apply to variables in procedural mode, that's not a supported syntax. And the other thing you are saying [is not true](https://3v4l.org/EQ9KX). – El_Vanja May 19 '21 at 11:19
  • If you are confused by a specific case, please edit the question and present it. I feel like we're potentially talking about two different things. – El_Vanja May 19 '21 at 11:42
  • In sum, I'm asking why do I have to precede a variable with a mark question to say that it can have a null value since I can assign a null value without preceding it by a mark question !! Or is there other reason for that, that I don't know!? – Hamid ER-REMLI May 19 '21 at 12:23
  • Please show a concrete example of what you're talking about. I have shown you an example that a class property without the null hint cannot be assigned null as a value. – El_Vanja May 19 '21 at 12:43
  • this is an example: `var = "S123"; echo "var of object = " . $obj->var . "
    "; $obj->var = null; echo "var of object after null assignment = " . $obj->var . "
    "; ?>` this outouts: var of object = S123 var of object after null assignment =
    – Hamid ER-REMLI May 19 '21 at 13:48
  • 1
    That property is not type-hinted. Adding a nullable hint `?` only makes sense if you're using type hinting in the first place. – El_Vanja May 19 '21 at 13:58
  • Ok, if I understand, a null can be assigned only to not type-hinted variables or those type-hinted but preceded by mark question !! If this is true, type-hinted variables can not be assigned a null value unless you add the mark question to the declaration!! wich explain the mark question's contribution !! is it ok? – Hamid ER-REMLI May 19 '21 at 14:23

1 Answers1

1

Variable $str can only be of type string:

string $str;

Variable $str can be of type string or null:

?string $str;
Casper André Casse
  • 573
  • 2
  • 8
  • 20
  • 1
    Please avoid creating duplicate content. This question has already been answered on Stack Overflow, there is no need to repeat the same thing over and over. You have sufficient reputation, you can flag a question as duplicate. – El_Vanja May 19 '21 at 10:18
  • Thank you Casper André Casse. In "Variable $str can be of type string or null", this means "null" is a type?? Since null could be assigned to a string, array or variable object, how could it be considered as a type? – Hamid ER-REMLI May 19 '21 at 10:30
  • 1
    @HamidER-REMLI null is a special data type which can have only one value: null. – Casper André Casse May 19 '21 at 11:50
  • Yes, thank you... **null** is a special data type that can be assigned to variables with diffrent types such as string, array and object. – Hamid ER-REMLI May 19 '21 at 14:38