Rahul Singh
Rahul Singh

Reputation: 1632

Passing and returnging array to the functions

I have created two arrays. I wanna pass these two array to any function. I am beginner with function so tried with rough code to achieve my task. As I have some values in $cntctnum and $cntcttype named array.

 $cntctnum      = array();
 $cntcttype     = array();
 $response = array();

 function play_with_array($cntctnum, $cntcttype){
 $contactnumber= $cntctnum[];
 $cntcttype = $cntcttype[];

 // some code to play with array.

 return resultarray();
 }


 $response = play_with_array($cntctnum, $cntcttype);

Is this right way to pass function in array? Is I need to declare $response as array before or when I return resultarray(), it will automatically consider it as array?

Upvotes: 0

Views: 64

Answers (3)

Alexey Gerasimov
Alexey Gerasimov

Reputation: 2141

Rahul, you pass array to a function just like you pass any other variable to a function. The reason to have a function is to have a block of code that can be re-used to perform the same set of calculations, etc.

It's hard to understand what you're trying to do inside your function. If this is something that's repeatable, meaning: compare array 1 vs array 2, get the bigger one, and return some sort of variation of that, then YES, create a function. If you have some code inside your function that is very specific to this page only and to these 2 arrays, you don't need the function. Just write the code inline.

Is I need to declare $response as array before or when I return resultarray(), it will automatically consider it as array?

No, you don't. If you resultarray variable inside a function is truly an array, you're good to go.

Upvotes: 0

Phill Pafford
Phill Pafford

Reputation: 85298

You have:

$cntctnum      = array();
$cntcttype     = array();
$response = array();

function play_with_array($cntctnum, $cntcttype){
$contactnumber= $cntctnum[]; // don't need []
$cntcttype = $cntcttype[];   // don't need []

// some code to play with array.

return resultarray(); // this is a function?
}


$response = play_with_array($cntctnum, $cntcttype);

I would do something like this

$cntctnum    = array();
$cntcttype    = array();

function play_with_array($contactnumber, $cntcttype){
    // some code to play with array.
    $response = array($contactnumber,$cntcttype);

    return $response; 
}

$response = play_with_array($cntctnum, $cntcttype);
echo "<pre>".print_r($response,true)."</pre>";

Upvotes: 0

halfdan
halfdan

Reputation: 34204

You don't need to define $response as array beforehand, but it might be a good idea to do so anyway depending on what the code afterwards expects.

In your function you don't return a function call resultarray() but a new array:

function play_with_array($cntctnum, $cntcttype) {
    $contactNumber = $cntctnum; // You don't need the assignment! Note: No brackets here!
    $resultArray = array();
    // Do something here        
    return $resultArray;
}

Upvotes: 1

Related Questions