freelance84 Posted June 29, 2011 Share Posted June 29, 2011 $results0to4 = array(); $results4to7 = array(); $results7to10 = array(); ////function to place the comment into the appropriate array: function arrayPlacer($comment,$aveScore){ if($aveScore < 4 && !in_array($comment,$results0to4)){ array_push($results0to4,$comment); return; } elseif($aveScore < 7 && !in_array($comment,$results4to7)){ array_push($results4to7,$comment); return; } elseif($aveScore <= 10 && !in_array($comment,$results7to10)){ array_push($results7to10,$comment); return; } } An $aveScore entering 'arrayPlacer' of value 5 results in the following error: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/mysite/public_html/script.php on line 192 Warning: array_push() [function.array-push]: First argument should be an array in /home/mysite/public_html/scritp.php on line 194 Therefore... is it not possible use an array in a function when the array was set out of the function? NB, the arrays on first run through are empty. Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/ Share on other sites More sharing options...
trq Posted June 29, 2011 Share Posted June 29, 2011 is it not possible use an array in a function when the array was set out of the function? No, functions have there own scope. You will need to pass the arrays in, and then return them. Why you are using 3 separate arrays though is beyond me. Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236340 Share on other sites More sharing options...
fugix Posted June 29, 2011 Share Posted June 29, 2011 you can pass your variables into the function using the "global" statement, or the $GLOBALS array..i.e $results0to4 = array(); function arrayPlacer($comment,$aveScore){ global $results0to4; if($aveScore < 4 && !in_array($comment,$results0to4)){ array_push($results0to4,$comment); return; } or $results0to4 = array(); function arrayPlacer($comment,$aveScore){ $results0to4 = $GLOBALS['results0to4']; if($aveScore < 4 && !in_array($comment,$results0to4)){ array_push($results0to4,$comment); return; } Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236344 Share on other sites More sharing options...
freelance84 Posted June 29, 2011 Author Share Posted June 29, 2011 Awesome, thank you! I'll give it a shot Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236353 Share on other sites More sharing options...
KevinM1 Posted June 29, 2011 Share Posted June 29, 2011 you can pass your variables into the function using the "global" statement, or the $GLOBALS array..i.e Which is a horrible way to pass in any parameter to any function, and definitely shouldn't be recommended. Simply put, if you use 'global' in any manner, you're doing it wrong. @OP - you have two legitimate options when passing a parameter into a function - by value, and by reference. Pass by value is what PHP uses by default (except with objects). Essentially, it creates a copy of the value you pass into the function. The value outside of the scope of the function is not affected unless it is assigned the return value of the function. Example: function addFive($value) { return $value + 5; } $num = 6; addFive($num); echo $num; // <-- will still be 6 since we didn't capture the return value. The COPY of $num was 11, briefly, but is destroyed upon function end since it's not returned $num = addFive($num); echo $num; // <-- will be 11 The other option is pass by reference, which sends the reference of the value to the function. This results in out-of-scope changes since you're doing more than simply sending a copy to the function. Example: function addFive(&$value) // <-- the '&' denotes reference { $value += 5; } $num = 6; addFive($num); echo $num; // <-- 11 The problem with dealing with references is that it creates side effects. They make it easy to write confusing functions that can have multiple simultaneous results. Use them with caution. More info: http://php.net/manual/en/language.references.pass.php http://www.php.net/manual/en/language.references.php Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236364 Share on other sites More sharing options...
freelance84 Posted June 29, 2011 Author Share Posted June 29, 2011 Hmm. Ok. I'm going to have a rethink on how the arrays are produced then. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236585 Share on other sites More sharing options...
fugix Posted June 30, 2011 Share Posted June 30, 2011 why cant you call the arrays in the functions themselves? Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236624 Share on other sites More sharing options...
xyph Posted June 30, 2011 Share Posted June 30, 2011 Globals create a debugging nightmare. You should never allow a function to change variables outside of their own scope, At least when a variable is passed by reference, you KNOW the function is interacting with it. On small scale projects, using globals may not become an issue. This doesn't give you an excuse to use them, though. Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236631 Share on other sites More sharing options...
freelance84 Posted June 30, 2011 Author Share Posted June 30, 2011 Wow... got the picture. NO GLOBALS. Never used globals before so i wont start using them now. The function was being called five times, for the time being i have abandoned the function, reduced the number of lines the function used and entered them manually in each of the five occurrences on the only script that uses them. Whilst this new feature settles in I will let the problem sit in the back of my head for a bit... see something may come to mind for a much more efficient way of doing it all. I'm now reading on the topics "Nightslyr" mentioned. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236728 Share on other sites More sharing options...
xyph Posted June 30, 2011 Share Posted June 30, 2011 Or you can just pass the arrays right into the function. function arrayPlacer($comment,$aveScore,&$results0to4,&$results4to7,&$results7to10){ if($aveScore < 4 && !in_array($comment,$results0to4)){ array_push($results0to4,$comment); return; } elseif($aveScore < 7 && !in_array($comment,$results4to7)){ array_push($results4to7,$comment); return; } elseif($aveScore <= 10 && !in_array($comment,$results7to10)){ array_push($results7to10,$comment); return; } } The & in the front says that anything the function does to the variable will be reflected in the variable calling it. IE $num = 5; echo $num .'<br>'; // outputs 5 timesTwo($num); echo $num; // outputs 10 function timesTwo( &$var ) { $var = $var * 2; } Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236731 Share on other sites More sharing options...
freelance84 Posted June 30, 2011 Author Share Posted June 30, 2011 ahhhhhh... something just went click in my head! Thank you! Right back to taking those lines back out and reintroducing the function Quote Link to comment https://forums.phpfreaks.com/topic/240706-calling-array-in-a-function-which-were-previously-set/#findComment-1236733 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.