2

I have function that will process some array and change it for example sort, remove some chars from it etc.

My question here is if it is better to use:

1. Reference parameter like this:

public function doStuff(&$data) {
    // $data processing here
}

and then my array will be changed without next assigment.

2. Calculate result and then return the result like this:

public function doStuff($data) {
    // $data processing here
    return $data;
}

So I need to assign result after processing.

Which is better (faster/recommended) solution to use? I have several arrays of 1000s records on which I want to apply my calculations.

1 Answer 1

2

If you are planning to modify original array then by reference or first approach should be selected, since it will work on original array and without creating a temporary variable to work thus saving memory and other processing time.

If you are planning to work with original as well as new array then go for by value or second approach.

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

Comments

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.