Jump to content

calling array in a function which were previously set


freelance84

Recommended Posts

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :headslap:

 

@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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  :D

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.