Jump to content

read in file array


Caitlin-havener

Recommended Posts

Why is the following printing "array"? My text document says "An apple a day keeps the doctor away."

<?php#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	$text=textloader("clichefile.txt");  // later, we will read this from										// a text file.	print"$text";?>

Link to comment
Share on other sites

That's what printing an array does... it prints "Array". If you expected a view of the actual array members and values, replace

print "$text";

with

var_dump($text);

Link to comment
Share on other sites

As boen said, since the type of $text is an array, that's what printing it is doing; printing out it's type. What you want to be doing (most likely) is print out the indexes in the array. As boen also said, you can see all data related to a variable, use var_dump. In the case of an array, it will show you all it's indexes/keys and their values. In this case, I bet to get what you want, you would want to have

print "$text[0]";

Link to comment
Share on other sites

Kay, I got that. I'm trying to figure out what the following notices mean:( ! ) Notice: Uninitialized string offset: 38 in C:\wamp\www\project2\project2.php on line 113( ! ) Notice: Uninitialized string offset: 45 in C:\wamp\www\project2\project2.php on line 164And when the user pushes "Next Phrase" I need "1" to be added to the session named "index" and then start the main program over again. Right now it keeps looping back to start endlessly even though "Next phrase" is pushed only once. How would I get the "if ($act=="next phrase") to work properly?

<?php	session_start();	$testnumber=2;		# A standard diagnostic tool	function logprint ($what,$num)	{global $testnumber;		if ($num==$testnumber)			print "LP:$what <br />";	}		#makeheader:	function makeheader( )	{		print '		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<h1>Moshell\'s Jeopardy Starter Kit</h1>		<form method="post">		';	} # makeheader		#makefooter:	function makefooter ()	{		print "</form></body></html>";	}	#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{			//logprint("i=$i,vi=".$visible[$i]." ltr=".$letters[$i],1);						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "<td><strong><center>*</center></strong></td>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";	}		######## MAIN PROGRAM ##########		makeheader();		$_SESSION['index']=0;	start();	function start(){	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];		$text=$array[$index];		print "$text";		for ($i=0; $i<=strlen($text);$i++)		{			$letters[$i+1]=strtoupper($text[$i]);			}			$letterlength=count($letters);			$visible=$_SESSION['visible'];	$guesses=$_SESSION['guesses'];		$act=$_POST['action'];	if (!$act)	{		logprint("noact",1);						for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;					$guesses=5;			} # initialization block			else if ($act=="GUESS")	{ // main repeated action. Get a guessletter, scan the array.		$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppecase		logprint("gp=$guessletter",1);				for ($i=1; $i<=$letterlength; $i++)		{			$ll=$letters[$i];			if ($guessletter==$letters[$i])			{				logprint("setting visible $i",1);				$visible[$i]=1;			}		}		$guesses=$guesses-1;			} #repeated action block		else if ($act=="NEXT PHRASE")	{		$_SESSION['index']+= 1;		start();			}		print "visible=$visible. Letters=$letters. Guesses=$guesses.";	display($visible,$letters);	drawinputscreen($guesses);					$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;}?>

Link to comment
Share on other sites

Oh I forgot to mention what this program does... It is sort of like jeopardy or hangman. It pulls a cliche phrase out of a file and the user guesses the letters and if the guess is correct the letter shows up in the table. I'm trying to get it to loop through the phrases in the file upon clicking "next phrase."Let me know if I'm not making sense or what not.

Link to comment
Share on other sites

What's the supposed logic behind this:

function start(){	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$text=$array[$index];		print "$text";		for ($i=0; $i<=strlen($text);$i++)		{			$letters[$i+1]=strtoupper($text[$i]);		}

?You're assigning the upper variant of each letter to the next member in the $letters array... how come? I mean... what's the deal?Also, I'm not sure if I'm interpreting it correctly... but I'm thinking you still haven't gotten the scope thing about variables in PHP. What's in one function is not available to other functions, and what's in the global scope is not visible to functions.... so your $letters variable actually doesn't even exists before the first iteration, and has no members.To fix the error, you can initialize the array. While you're at it, you might as well use the indexes starting from 0, like:

function start(){	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();	$text=$array[$index];		print "$text";		for ($i=0; $i<=strlen($text);$i++)		{			$letters[]=strtoupper($text[$i]);		}

or wait... why not strtoupper() the whole string? Why loop? Why not:

function start(){	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$text=$array[$index];	$letters=strtoupper($text);

Link to comment
Share on other sites

What's the supposed logic behind this:
function start(){	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();//insert function to display if no cliches are left				$text=$array[$index];		//$letters=strtoupper($text);		print "$text";		for ($i=0; $i<=strlen($text);$i++)		{			$letters[$i]=strtoupper($text[$i]);				//how r we making text into array of array		}

Link to comment
Share on other sites

Oh and could the problem be that it is not breaking up the phrase into an array of each letter? I'm not sure how to do this if so. My professor's version worked fine with similar code. I just added the capability of reading in a file. He had $text="A whole sentence of words"; // later, we will read this from // a text file. for ($i=0; $i<=strlen($text);$i++) { $letters[$i+1]=strtoupper($text[$i]); }

Link to comment
Share on other sites

$text=$array[$index];

it should be

$text=$array["$index"];

otherwise $index will not evaluate any value

Link to comment
Share on other sites

and one more thing.if i am not missunderstanding....in your code i think $text is a string

$text=$array[$index];

so i think you cant take out letter by indexing the $text["$somekey"].

$letters[$i]=strtoupper($text[$i]

); in other languages such as c/c++ string reffer to a character and a word or sentence is a array of string. but in php the whole word or a whole sentence can reffer to string (and act as so). so if you want to get the value of each element of $text by index...you need to transform it to an array using [url http://php.net/function.str_split[/url]

Link to comment
Share on other sites

Actually, both of the things you said are not right.If $index has the value 0, then

$array[$index]

is evaluated to

$array[0]

which in turn is evaluated to whatever the first element in the array is.

$array["$index"]

will be evaluated to

$array['0']

which I think PHP will automatically cast to an integer in the absence of a string key of the same name.And as for the using strings as arrays, see "String access and modification by character".As for the issue... I'm not sure... I'm losing sight of the flow behind the whole thing.

Link to comment
Share on other sites

Actually, both of the things you said are not right.If $index has the value 0, then
$array[$index]

is evaluated to

$array[0]

which in turn is evaluated to whatever the first element in the array is.

$array["$index"]

will be evaluated to

$array['0']

which I think PHP will automatically cast to an integer in the absence of a string key of the same name.And as for the using strings as arrays, see "String access and modification by character".As for the issue... I'm not sure... I'm losing sight of the flow behind the whole thing.

yes actualy i use double quotes when variables are used in array key so when it is not int it dont throw error and if its int it get type casted. as i was not totaly sure about the purpose of the code.(was not sure its assoc or numeric) so i thought it would be the reason.and thanks for the second link. did not know about... that it also possible.
Link to comment
Share on other sites

yes actualy i use double quotes when variables are used in array key so when it is not int it dont throw error and if its int it get type casted. as i was not totaly sure about the purpose of the code.(was not sure its assoc or numeric) so i thought it would be the reason.and thanks for the second link. did not know about... that it also possible.
Comments are there to explain what a piece of code is for. If you think it's important to know whether it's associative or not then put a comment in your code.To prevent strings from being used as a key for a numeric array you can use intval() on the index. I, personally, try to avoid type juggling when possible.
Link to comment
Share on other sites

Okay- I made what I believe were you all's suggested changes.. and it didn't make a bit of difference. All the warnings are pointing to something wrong with the "$letters[$i]" that occurs here:

for ($i=0; $i<=strlen($text);$i++)		{			$letters[$i]=strtoupper($txtarr[$i]);			}

It says its offset. I'm really not sure what that means. Also when I get the string length printed out it is saying its two longer than it actually is, what gives? Any who, still having the biggest issue when I press "next phrase." It just prints out all the phrases I have in clichetext.txt and a bunch of warnings that say that the above code, the line with "$letters[$i]=strtoupper($txtarr[$i]);" is offset. So it is apparently the issue but I can't figure it out. Boy it sucks to be new to this! The whole thing for reference:

<?php // woffer02.php	session_start();	$testnumber=2;		# A standard diagnostic tool	function logprint ($what,$num)	{global $testnumber;		if ($num==$testnumber)			print "LP:$what <br />";	}		#makeheader:	function makeheader( )	{		print '		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<link rel="stylesheet" type="text/css" href="style.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<div id="wrap">		<h1>Caitlin\'s Jeopardy</h1>		<form method="post">		';	} # makeheader		#makefooter:	function makefooter ()	{		print "</div></form></body></html>";	}	#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{			//logprint("i=$i,vi=".$visible[$i]." ltr=".$letters[$i],1);						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "</tr></table><br/>				<table class='mtable'><tr>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";	}		######## MAIN PROGRAM ##########		makeheader();		$_SESSION['index']=0;	$_SESSION['turn']="GOLD";		start();	function start(){	$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();//insert function to display if no cliches are left				$text=$array["$index"];		//$letters=strtoupper($text);		print "$text";		$txtarr=str_split($text);		$x=strlen($text);		print "$x";		for ($i=0; $i<=strlen($text);$i++)		{			$letters[$i]=strtoupper($txtarr[$i]);				//how r we making text into array of array		}			$letterlength=count($letters);			$visible=$_SESSION['visible'];	$guesses=$_SESSION['guesses'];		$act=isset($_POST['action']) ? $_POST['action'] : '';	if (!$act)	{		logprint("noact",1);						for ($i=1; $i<=$letterlength; $i++)			$visible[$i]=0;					$guesses=5;			} # initialization block			else if ($act=="GUESS")	{ // main repeated action. Get a guessletter, scan the array.		$turn=$_SESSION['turn'];						//WHOS TURN??		if ($turn=="GOLD"){			print"It is $turn team's turn.";			$_SESSION['turn']="BLACK";		}		else{			print "It is $turn team's turn.";			$_SESSION['turn']="GOLD";		} #End whos turn					  		$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppecase		logprint("gp=$guessletter",1);				for ($i=1; $i<=$letterlength; $i++)		{			$ll=$letters[$i];			if ($guessletter==$letters[$i])			{				logprint("setting visible $i",1);				$visible[$i]=1;			}		}//IF LETTER IS NOWHERE TO BE FOUND. use RE				$guesses=$guesses-1;			} #repeated action block		else if ($act=="NEXT PHRASE")	{		$_SESSION['index']+= 1;		start();			}		print "visible=$visible. Letters=$letters. Guesses=$guesses.";	display($visible,$letters);	drawinputscreen($guesses);					$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;}?>

Link to comment
Share on other sites

Oh, and when "GUESS" is pushed more offset warnings come up for the lines that have the $letters[$i] in the following clip

for ($i=1; $i<=$letterlength; $i++)		{			$ll=$letters[$i];			if ($guessletter==$letters[$i])			{				logprint("setting visible $i",1);				$visible[$i]=1;			}		}

Link to comment
Share on other sites

I don't want to distract from the previous questions, that still need answering... but I have moved on to working on a smaller part of the program and have a quick question. How would you store an array inside of a session then print out that session array? I am trying to print out the guessed letters that are not in the phrase (kinda like hangman). Do you see what my mistake is with this? My logic with the bad_guess_index is that each time the guessletter is wrong it will store it in a different element of the guess_array.

$_SESSION['bad_guess_idex']=0;if (!preg_match("/$guessletter+/",$text)){				$guess_index=$_SESSION['bad_guess_idex'];				$_SESSION['guess_array[$guess_index]']=$guessletter;				$_SESSION['bad_guess_idex'] += 1;	}		$guess_array_print=$_SESSION['guess_array'];	print"<div id='bad_guesses'>";	print"$guess_array_print";	print"</div>";

Link to comment
Share on other sites

You can use arrays within the $_SESSION variable as normal two dimensional arrays.

$_SESSION['guess_array[$guess_index]']

always targets a session variable that has the key

guess_array[$guess_index]
When you first start the session, you should initialize the array, like:
$_SESSION['guess_array'] = array();

and after that, in your code, you could have

$_SESSION['guess_array'][$guess_index] = $guessletter;

Link to comment
Share on other sites

You can use arrays within the $_SESSION variable as normal two dimensional arrays.
$_SESSION['bad_guess_index']=0;	$_SESSION['guess_array'] = array();

I changed the inner code to:

if (!preg_match("/($guessletter)/",$text)){				$guess_index=$_SESSION['bad_guess_index'];				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				print"$guess_index <br/>";	}		print"<div id='bad_guesses'>";	print"$guess_index <br/>";	for($i=0; $i<=$guess_index; $i++){		$bad_guess_print=$_SESSION['guess_array'];		print"$bad_guess_print[$i]";

As you can see I printed the index and it is not changing from "0" when guess is pushed again. How come? Also- something must be up with my preg_match $guessletter. "R" only occurs once in the text but it still prints it out as a letter that is not in the text. "A" occurs several times and does not print. "Q" never occurs and prints.Issue 2:These have remained unanswered for some time regarding letters[$i], I will keep trying to figure it out please help if you can. See posts #15-16 (a couple back).

Link to comment
Share on other sites

The initialization must only happen at the start of the session, not on every page load. The way to check if the session has just started is to check if it's empty at the top, when doing session_start(), like:

<?phpsession_start();if (empty($_SESSION)) {	//Initialize session variables	$_SESSION['bad_guess_index'] = 0;	$_SESSION['guess_array'] = array();}

This might be a possible cause for some of your other issues as well.

Link to comment
Share on other sites

The initialization must only happen at the start of the session, not on every page load. The way to check if the session has just started is to check if it's empty at the top, when doing session_start(), like:
<?php // woffer02.php	session_start();	if (empty($_SESSION)) {    //Initialize session variables    $_SESSION['bad_guess_index'] = 0;    $_SESSION['guess_array'] = array();	$_SESSION['index']=0;	$_SESSION['turn']="GOLD"; //GOOD}	$testnumber=2;		# A standard diagnostic tool	function logprint ($what,$num)	{global $testnumber;		if ($num==$testnumber)			print "LP:$what <br />";	}		#makeheader:	function makeheader( )	{		print '		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">		<html xmlns="http://www.w3.org/1999/xhtml">		<head>		<link rel="stylesheet" type="text/css" href="style.css" />		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />		<title></title>			<style type="text/css">			.mtable td 			{				width:50px;			}		</style>			</head>				<body>		<div id="wrap">		<h1>Caitlin\'s Jeopardy</h1>		<form method="post">		';	} # makeheader		#makefooter:	function makefooter ()	{		print "</div></form></body></html>";	}	#textloader:		function textloader($whatfile)		{				if (!file_exists($whatfile))		{			print "Sorry, can't find $whatfile.";			exit;		}		else		{			$textarray=file($whatfile);			return $textarray;			}		}# textloader	#display: Draws the Jeopardy table	function display($visible, $letters)	{		$letterlength=count($letters);		print "<table class='mtable' border=1><tr>";		for ($i=1; $i<$letterlength; $i++)		{			//logprint("i=$i,vi=".$visible[$i]." ltr=".$letters[$i],1);						//print "i=$i,visible=".$visible[$i]."<br />";			if ($letters[$i]==' ')				print "</tr></table><br/>				<table class='mtable'><tr>";			else if ($visible[$i])			 {				print "<td><center>".$letters[$i]."</center></td>";			 }			else				print "<td> </td>";		}		print "</tr></table>";	}		#drawinputscreen: Asks for input.	function drawinputscreen($guesses)	{		print "<input type='text' name='guessletter'>";		print "Guess a letter. You have $guesses guesses left.";		print "<input type='submit' name='action' value='GUESS'>";		print "<input type='submit' name='action' value='REVEAL'>";		print "<input type='submit' name='action' value='NEXT PHRASE'>";	}		######## MAIN PROGRAM ##########		makeheader();	start();	function start(){		$array= textloader("clichefile.txt");	$index=$_SESSION['index'];	$letters = array();		$text=$array[$index];//function to display if no cliches are left		if ($text==''){			$text="There are no more phrases left.";		}		//$letters=strtoupper($text);		print "$text";		$txtarr=str_split($text);		$x=strlen($text);		print "$x";		for ($i=0; $i<=strlen($text);$i++)		{			$letters[$i+1]=strtoupper($txtarr[$i]);			}			$letterlength=count($letters);			$visible=$_SESSION['visible'];	$guesses=$_SESSION['guesses'];		$act=isset($_POST['action']) ? $_POST['action'] : '';	if (!$act)	{		logprint("noact",1);				//if no more phrases print it in cells and set all to visible		if ($text=="There are no more phrases left."){			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=1;		}		else{			for ($i=1; $i<=$letterlength; $i++)				$visible[$i]=0;		}				$guesses=5;			} # initialization block			else if ($act=="GUESS")	{ // main repeated action. Get a guessletter, scan the array.							  		$guessletter=strtoupper($_POST['guessletter']); // a-> A, etc. uppecase		logprint("gp=$guessletter",1);				for ($i=1; $i<=$letterlength; $i++)		{			$ll=$letters[$i];			if ($guessletter==$letters[$i])			{				logprint("setting visible $i",1);				$visible[$i]=1;			}		}				$turn=$_SESSION['turn'];		//WHOS TURN??		if ($turn=="GOLD"){			//print"It is $turn team's turn.";			$_SESSION['turn']="BLACK";		}		else if($turn=="BLACK"){			//print "It is $turn team's turn.";			$_SESSION['turn']="GOLD";		} #End whos turn//IF LETTER IS NOWHERE TO BE FOUND. 							$guesses=$guesses-1;			if (!preg_match("/($guessletter)/",$text)){				$guess_index=$_SESSION['bad_guess_index'];				$_SESSION['guess_array'][$guess_index] = $guessletter;				$_SESSION['bad_guess_index'] += 1;				print"$guess_index <br/>";	}	$guess_index=$_SESSION['bad_guess_index'];	print"<div id='bad_guesses'>";	print"$guess_index <br/>";	for($i=0; $i<=$guess_index; $i++){		$bad_guess_print=$_SESSION['guess_array'];		print"$bad_guess_print[$i]";	}	print"</div>";		} #repeated action block		else if ($act=="NEXT PHRASE")	{		$_SESSION['index']+= 1;		unset($_SESSION['bad_guess_index']);		unset($_SESSION['guess_array']);		start();			}	$turn_print=$_SESSION['turn'];	print"It is $turn_print team's turn.";	print "visible=$visible. Letters=$letters. Guesses=$guesses.";	display($visible,$letters);	drawinputscreen($guesses);					$_SESSION['visible']=$visible;	$_SESSION['guesses']=$guesses;}?>

Link to comment
Share on other sites

Strange... it does output something from the text file on my computer... I'm not sure if it's the very thing you're expecting, at the point you're expecting, but still.I do see one notice that I can track though:

for ($i=0; $i<=strlen($text);$i++)

strings start from 0 to the length of the string -1. So, replace that with

for ($i=0; $i<strlen($text);$i++)

(who knows... the rest of your errors so far have been from minor things like that, so this might as well be too)

Link to comment
Share on other sites

Strange... it does output something from the text file on my computer... I'm not sure if it's the very thing you're expecting, at the point you're expecting, but still.I do see one notice that I can track though:
for ($i=0; $i<=strlen($text);$i++)

strings start from 0 to the length of the string -1. So, replace that with

for ($i=0; $i<strlen($text);$i++)

(who knows... the rest of your errors so far have been from minor things like that, so this might as well be too)

Ahh, it is not the position of the index initialization that is causing it not to read from clichefile.txt. When you push "next phrase" it is looping through the whole file and saving it in the session that you did so. So my if ($text=="") print "No more phrases" is in effect. I'm still getting one more cell printing that is not there. But the biggest issue with the program still looms... when "next phrase" is pushed it just prints out everything in the text file and has an infinite error message of undefined offset line 118 which is:"$text=$array[$index]" (where it assigns one line of text from the file to 'text' variable)."
Link to comment
Share on other sites

At

	$text = $array[$index];//function to display if no cliches are left	if ($text == '') {		$text = "There are no more phrases left.";	}

You're unconditionally assuming that $text is going to get a value... it's not going to get if $index doesn't evaluate to an index within $array, so you might want to do the check like:

	$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";

This literally means "if the index exists in the array, get the value at that index and set $text to it, otherwise, set $text to "There are no more phrases left.".

Link to comment
Share on other sites

At
	$text = $array[$index];//function to display if no cliches are left	if ($text == '') {		$text = "There are no more phrases left.";	}

You're unconditionally assuming that $text is going to get a value... it's not going to get if $index doesn't evaluate to an index within $array, so you might want to do the check like:

	$text = array_key_exists($index, $array) ? $array[$index] : "There are no more phrases left.";

This literally means "if the index exists in the array, get the value at that index and set $text to it, otherwise, set $text to "There are no more phrases left.".

Made the change but still getting undefined offset line 118 which is:"$text=$array[$index]"
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...