2

I use a little bit of php to work with a database. Never used it enough to explore what it can do. Until now. I went looking at the documentation and I see a function I have used countless times. I thought I knew how to use it until the docs and now I'm baffled

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

In my php that is simply

mysqli_query($connection, $anMySQLQuery);

Where I'm confused is the use of

[, int $resultmode = MYSQLI_STORE_RESULT ]

What's with the [] and the comma after the opening [?

As a reader of the docs what does [, indicate ?

3 Answers 3

3

It's not related to php, lots of programming languages / libraries use this convention to tell us the parameter(s) beetween [] are optionnal.

foo(bar[,baz]) means the function foo takes at least 1 argument, bar and an optional baz parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah ok excellent. Only time i have seen [] is objective-c and there they didn't mean optional. Thanks for the quick answer. Will accept it as when it lets me
For more information, see the chapter How to read a function definition (prototype) in the manual.
2
[, int $resultmode = MYSQLI_STORE_RESULT ]

The comma is the parameter separater that will be required if passing the second parameter.

The brackets denote an 'optional' parameter, and there may be many of these.

The 'int' is the 'type' of parameter that will be accecpted.

The value 'MYSQLI_STORE_RESULT' is the default value that will be used if the parameter is not added. (In this case, a CONSTANT).

Comments

1

Square brackets ([]) are used to indicate optional parameters.

So the argument list should be interpreted as follows: You must provide $link, followed by a comma, followed by $query. Then you may add another comma and $resultmode. If you do not pass the third argument, it will be assigned a default value of MYSQLI_STORE_RESULT.

1 Comment

Thank you. Marked your answer up but you were just beaten to it so have to give the check mark to Junius Rendel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.