gsingh85 Posted July 3, 2014 Share Posted July 3, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289405-confusion-over-an-array-within-a-function-parameter/ Share on other sites More sharing options...
trq Posted July 3, 2014 Share Posted July 3, 2014 This syntax defines a default argument. Meaning, if you pass no argument to the myFunction function, $myVariable will be an empty array. See http://php.net/manual/en/functions.arguments.php Quote Link to comment https://forums.phpfreaks.com/topic/289405-confusion-over-an-array-within-a-function-parameter/#findComment-1483661 Share on other sites More sharing options...
Raz3rt Posted July 3, 2014 Share Posted July 3, 2014 (edited) 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 July 3, 2014 by Raz3rt Quote Link to comment https://forums.phpfreaks.com/topic/289405-confusion-over-an-array-within-a-function-parameter/#findComment-1483663 Share on other sites More sharing options...
ginerjm Posted July 3, 2014 Share Posted July 3, 2014 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/289405-confusion-over-an-array-within-a-function-parameter/#findComment-1483721 Share on other sites More sharing options...
Solution gsingh85 Posted July 4, 2014 Author Solution Share Posted July 4, 2014 That's great. Thanks so much for your help guys. Quote Link to comment https://forums.phpfreaks.com/topic/289405-confusion-over-an-array-within-a-function-parameter/#findComment-1483880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.