Should you initialize $matches before calling preg_match?

php, regex

Solution

There is no need to initialise $matches as it will be updated with the results. It is in effect a second return value from the function.

Problem

`preg_match` accepts a `$matches` argument as a reference. All the examples I've seen do not initialize it before it's passed as an argument. Like this: ``` preg_match($somePattern, $someSubject, $matches); print_r($matches); ``` Isn't this error-prone? What if `$matches` already contains a value? I would think it should be initialized to an empty array before passing it in as an arg. Like this: ``` $matches = array(); preg_match($somePattern, $someSubject, $matches); print_r($matches); ``` Am i just being paranoid?

Original source