LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2004, 11:04 PM   #1
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
Static Arrays in php Not working


im trying to create a static array in a function that checks to see if the card has already been played this turn. So i have class and a method which generates a random number from 0 to 19 and then calls another method that tests that number against an array. The array starts blank but each time a number is checked if its not found in the array it gets pushed onto the array. But even though i have the array set as static it gets reset everytime i call the function can someone tell me if the code is wrong or what would you recommend for storing and checking numbers so that they dont get used more then once
Code:
<?php
//Function gets passed a number and checks if it exists in an array and returns true or false
function check_num($new_num){
			
			static $used_nums = array();//Static array for storing numbers
			
			//this is a print test, prints out the full array each time the array is checked
			while(list($key, $val) = each($used_nums))
			{
				echo $val;
				echo " ";
			}
			echo "<br>";
			
			//Check array to see if number is already present
			while(list($key, $val) = each($used_nums))
			{
				if($new_num == $val)
				{
					//if number is found return true
					return true;
				}
			}
			
			//if reached here then number isnt in array so add it
			$used_nums = array_push($used_nums, $new_num);
			//and return false to let caller know it didnt exist already
			return false;			
}
?>
 
Old 03-04-2004, 08:53 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
if u call the script more than once u better store the array in a $_SESSION['var']
see documentation on session_functions

sth like
<?php
session_start();
if (! is_array($_SESSION['cards'])) $_SESSION['cards'] = array();
?>
might solve the problem
or am i getting u wrong
 
Old 03-04-2004, 12:04 PM   #3
elitecodex
Member
 
Registered: Feb 2004
Location: MD
Distribution: Fedora 9
Posts: 129

Rep: Reputation: 15
j-ray is right in my opinion.

If memory serves me right, the static will only apply to each call of the function in that specific request. Once the script is done and the HTML rendered, when it is called again... its another request.

You can either use the session variables or hide the numbers used in an INPUT tag in the HTML (hidden of couse). Either way, that looks to me the only thing I can think of.
 
Old 03-04-2004, 03:44 PM   #4
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Original Poster
Rep: Reputation: 15
its more like a card game its generating sets of 5 random numbers depending on how many hands i want it to show so i have a class for creating the hand. one method generates a random number checks it against the other numbers generated that turn(the function i showed in the first post) if its unique it adds it to the array of used numbers(the one that i need to be static) then returns true or false if it returns false then the calling function(random number function) adds that number to the persons hand and loops to generate another number and does the same process. The use would be like this
Code:
myhand = new handofcards();
anotherhand = new handofcards();
so that would generate two hand objects each with five unique numbers i have other methods to display and compare but im not having a problem with those. But technically it could be used to generate any amount of hands as long as there are enough cards(right now its just limited to two or three to avoid running out of cards)

Last edited by ElementNine; 03-04-2004 at 03:46 PM.
 
Old 03-05-2004, 10:29 AM   #5
elitecodex
Member
 
Registered: Feb 2004
Location: MD
Distribution: Fedora 9
Posts: 129

Rep: Reputation: 15
hmmm...

Im looking at your code again and might have the solution.

Everytime you call the function, you are declaring a static variable and setting it to an empty array. So it is infact static, but its being emptied by a "new" array each time.

Try something like this

Code:
<?php

function myfunc($num)
{
    static $myarray;

    if ( is_null($myarray) )
        $my_array = new array();

    // ..... Rest of code
}
Something along those lines should do it... I didnt catch that on the first post. This way, the array is only null when you define it (with the static keyword) and when you add values to it, is_null will return false and it wont be re-declared; thus wont be set to an empty array.

Sounds like I repeated myself there a few times, but you get the idea

Hope this helps!

Will
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php classes, arrays towlie Programming 10 06-07-2005 11:10 AM
Arrays, checkboxes in javascript & PHP Elijah Programming 21 11-15-2004 11:37 PM
php arrays and mysql simpleguy Programming 2 07-08-2004 06:28 AM
PHP - Reading data from multidimentional arrays and mail it. zyrtech Programming 8 08-09-2003 08:03 PM
php - include, require and arrays gui10 Programming 1 02-05-2002 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration