0

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?

3
  • Those really look like misspelling-proof variable names Commented Nov 18, 2011 at 14:34
  • My aim is not to correct my spelling... I have created a dummy function for rough idea to my needs.. Commented Nov 18, 2011 at 14:35
  • Couldn't you come up with simpler names like $array_test or $array_one? in 'cntcttype' it's easy to mispell a letter and raise errors Commented Nov 18, 2011 at 14:38

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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>";

Comments

0

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.

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.