Why is in_array() giving me an error?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Why is in_array() giving me an error?

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

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

Post 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!
phppro62
Forum Newbie
Posts: 16
Joined: Wed Oct 27, 2010 2:45 am

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

Post 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
Last edited by Eran on Wed Oct 27, 2010 3:42 pm, edited 1 time in total.
Reason: Added [syntax=php][/syntax] tags. Please use those in the future
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

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

Post 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.
Post Reply