Jump to content

Confusion over an array within a function parameter


Go to solution Solved by gsingh85,

Recommended Posts

I'm fairly new to this and I have looked around but I can't find much information on it. Basically I know what an array and a function is but I'm currently working through an oop login and register sytem and I am seeing this quite a lot:

 

function myFunction($myVariable = array()){

 

$Firstvariable = $myVariable[0];

$Secondvariable = $myVariable[1];

 

//etc

 

}

 

This is really confusing me and I can't see any examples in any tutorial sites or books of an array and a function being used like this. Can someone just explain how something like this would work in simple terms? I have also checked the manual but I can't see an example an array being used like this. Any help would be much appreciated. Thanks.

Maybe an example wil help you out?

function myFunction($myVariable = array()){           //$myVariable represends the array you have created when calling the function (Note: You can also do this with all values into a different $variable but learn working with arrays that means less code <=> faster!

$Firstvariable = $myVariable[0];              // So blue wil be added to $Firstvariable
$Secondvariable = $myVariable[1];        // green wil be added to $Secondvariable
$Thirthvariable = $myVariable[2];        // yellow wil be added to $Secondvariable
$Fourthvariable = $myVariable[3];        // white wil be added to $Secondvariable

echo "my favorite colors are $Firstvariable and $Secondvariable also $Thirthvariable and especially $Fourthvariable";

}

myFunction(array("blue", "green", "yellow", "white"));     // outputs "my favorite colors are blue and green also yellow and especially white"

There are however much better ways to do this. Try working with for and foreach loops to iterate (run over/through) an array. But maybe when you just started to work you better write the code this way! Good luck! Took me 2 years to almost fully understand the code...

Edited by Raz3rt

Your function has some faults with it. 

1 - If you are always going to feed an array of 4 values to the function then you should check for that in the function by using count() before attempting to process the input data.

2 - If you are NOT always going to have 4 values, then you need to change your function code to handle a variable number fo inputs (in the array).  The foreach() would be ideal for this purpose.

3 - personally I would never use a default value in my function header of simply "array()".  Setting the parm to a simple 'null' is sufficient for the ensuing code to check before continuing to process.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.