Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic parameter questions

I just started learning PHP and I have some questions about the parameters. Consider DateTime class PHP DateTime Class Manual

public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
public DateTime setDate ( int $year , int $month , int $day )

Here's my question:

  1. Why the parameters in the constructor are in brackets when setDate parameters aren't in brackets?
  2. Why there's bracket within a bracket?
  3. Why there's comma before the open bracket, [, ?

Thanks in advance.

like image 644
Harry Cho Avatar asked Dec 03 '25 19:12

Harry Cho


2 Answers

  1. Parameters within square brackets are optional.
  2. Some optional parameters can only be specified if you specify other optional parameters.
  3. The comma is only required if you specify the second optional parameter.
like image 146
Ignacio Vazquez-Abrams Avatar answered Dec 05 '25 10:12

Ignacio Vazquez-Abrams


  1. The parameters in brackets are optional. Therefore, if I initialize the DateTime class and don't provide the second parameter, it takes the default value of null.

  2. Nested brackets just means this: If I were to initialize the DateTime class, I provid either parameter 1 or parameter 1 and 2, or none at all. I cannot opt to provide no parameter 1, but a value for parameter 2.

  3. The comma just seperates the different parameters in the function's signature if more than 1 is provided..

like image 44
F21 Avatar answered Dec 05 '25 08:12

F21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!