Get array value from other function

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
husniteja
Forum Newbie
Posts: 10
Joined: Fri Feb 25, 2005 8:33 pm
Location: south korea

Get array value from other function

Post by husniteja »

I have two function which the first function for read file and the second for view

Code: Select all

function readFile(){
	$fd = fopen("guest.dat","r");
	$i=0;
	while(!feof($fd)){
	      $i++;
	      $name[] = trim(fgets($fd),255);
	      $comment[] = trim(fgets($fd),255);
	      $space[] = trim(fgets($fd),255);
	}
	fclose($fd);
}

function view(){
	global $HTTP_GET_VARS;
	$pageLength = 5;
	$this->readFile();

	if(isset($HTTP_GET_VARS["start"]) && is_numeric($HTTP_GET_VARS["start"])){
	$mulai = $HTTP_GET_VARS["start"];
			}else{
				$start = 0;
			}

	for($i=$start; $i<$start+$pageLength&& $i<$this->readFile();$i++){
		//Want to print the array from readFile

	          }
}
I want to print the value of array from readFile(), i have tried with the same function and it's no problem, in the same function i just use the for statement to print name, coment and space.

But i want to separate my function, and i don't know how to call the name, comment and space in view function. I use

Code: Select all

echo $this->readFile().name[$i];
may be i am wrong because there no meaning for this statement


feyd | hey look ma,

Code: Select all

is on. [/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remove line 14.

You never set the data built in readFile() to anywhere outside the function. $start is only set in one code path. You don't return anything from readFile().
husniteja
Forum Newbie
Posts: 10
Joined: Fri Feb 25, 2005 8:33 pm
Location: south korea

Post by husniteja »

hi feyd

Oke, may be my code is not complete, i still use the global and $start.

And i have tried to get the return value from readFile(), i use return $i (not show in the last code). but since i just return the $i, how to call the name, comment and space ?
Post Reply