Page 1 of 1

Why is in_array() giving me an error?

Posted: Wed Oct 27, 2010 3:10 pm
by s992
Relevant code:

Code: Select all

$cycle = array('Feb-10','May-10','Aug-10','Nov-10');

function printDate($customer) {
	$query = "SELECT date FROM $customer ORDER BY date";
	$result = mysql_query($query) OR die(mysql_error());
	$num = mysql_num_rows($result);
	$x = 0;
	echo "<col />";
	while($x < $num) {
		$data = mysql_result($result,$x,'date');
		$date = date('M-y',strtotime($data));
		$colDate = (in_array($date,$cycle)) ? '<col style="border-left:solid 3px" />' : '<col />'; // Error here.
		echo $colDate;
		$x++;
	}
Receiving the following error message:
[text]
Notice: Undefined variable: cycle in C:\wamp\www\display3.php on line 41

Warning: in_array() expects parameter 2 to be array, null given in C:\wamp\www\display3.php on line 41
[/text]

var_dump() returns the array $cycle just fine. What am I missing?

Re: Why is in_array() giving me an error?

Posted: Wed Oct 27, 2010 3:21 pm
by Eran
functions in PHP don't have access to the global scope. $cycle is defined in the global scope but is not defined in the function scope. Either make it a global variable (though you probably shouldn't if you're not aware of the implications), pass it into the function or declare it inside the function.

If you want to reuse the $cycle array between several functions, consider using a class

Re: Why is in_array() giving me an error?

Posted: Wed Oct 27, 2010 3:34 pm
by s992
Perfect, thank you. I do plan on using it in classes eventually, but right now I'm just writing out some concept code to make sure I know what I'm doing.

Anyway - problem solved by moving it into the function!

Re: Why is in_array() giving me an error?

Posted: Wed Oct 27, 2010 3:37 pm
by phppro62
hi mate
What Eran said is 100% right and also you forgot to close the function }
at the end

this is your code

Code: Select all

function printDate($customer) {
        $query = "SELECT date FROM $customer ORDER BY date";
        $result = mysql_query($query) OR die(mysql_error());
        $num = mysql_num_rows($result);
        $x = 0;
        echo "<col />";
        while($x < $num) {
                $data = mysql_result($result,$x,'date');
                $date = date('M-y',strtotime($data));
                $colDate = (in_array($date,$cycle)) ? '<col style="border-left:solid 3px" />' : '<col />'; // Error here.
                echo $colDate;
                $x++;
        }
}
you need to add one } more at the end

the first one for While and the second one for the function

Re: Why is in_array() giving me an error?

Posted: Wed Oct 27, 2010 3:44 pm
by Eran
He just showed partial code. Without that closing brace, he would have received a fatal error and never would've seen that warning he gave in his first post.

By the way, I edited your post to add [ syntax] tags. Please use those in the future

Re: Why is in_array() giving me an error?

Posted: Wed Oct 27, 2010 3:57 pm
by s992
phppro62 wrote:hi mate
What Eran said is 100% right and also you forgot to close the function }
at the end

you need to add one } more at the end

the first one for While and the second one for the function
Right, I appreciate your pointing that out - but Eran is correct in that it was just partial code. Thanks again, guys.