0

I am new to php, so don't know much. I the php DOCS for example fwrite :

int fwrite ( resource $handle , string $string [, int $length ] )

what does [ and ] denote ? I see them everywhere. What do they stand for ?

Y.E.P
  • 1,187
  • 6
  • 20
  • 40

4 Answers4

6

It means that the parameter is optional. There’s usually a default specified after the parameter name using = (default), and details down in the description.

In the case of fwrite, that’s:

If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • but then why `[` and `]` ? Why not just `]` or just `[` ? – Y.E.P Mar 13 '13 at 14:14
  • 5
    @Y.E.P That makes no sense to me. – Kermit Mar 13 '13 at 14:15
  • @Y.E.P: The brackets surround the optional parameter(s)… if it were just `]` you wouldn’t know where it started, and just `[` looks funny. :) Not to mention that sometimes they’re nested. – Ry- Mar 13 '13 at 14:15
  • @Y.E.P Because they're brackets and they visually resemble a box. It's the part inside the [ box ] that's optional. – Boann Mar 13 '13 at 14:16
4

It means optional arguments to the method, you don't have to pass them.

Generally, there is a default shown, for example in the case of preg_quote, the default is NULL:

string preg_quote ( string $str [, string $delimiter = NULL ] )

If you're happy with that default, you can omit the argument. Another good example would be mktime.

int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

Note that in mktime's documentation, it's stated that you can skip parameters using a specific format:

Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
2

It stands for an optional argument. If you don't specify it, default value will be used. It will say in the documentation which one it is, depending on the function.

Sometimes you will want to pass optional parameters to a function, for example if you want to trim all slashes on the right, you can pass optional argument to the function

$string = rtrim($string, '/');

Without optional parameter '/', whitespace would be trimmed

Marko D
  • 7,576
  • 2
  • 26
  • 38
0

It means it's an optional parameter.

karmafunk
  • 1,453
  • 12
  • 20
  • 4
    Can you please expand on your answer? Your answer does not add anything new that hasn't been already stated. – Kermit Mar 13 '13 at 14:17
  • Sorry, it was in my browser and I thought I was the first answer. It's a simple answer to a simple question. Makes perfect sense to me but I guess some people are less fortunate. – karmafunk Mar 13 '13 at 14:36