CSmith1128 Posted May 7, 2007 Share Posted May 7, 2007 Hello. I need some help with arrays. I am trying to get the contents of a file and put them into an array. I have everything working so far, except when I want to compare the contents of an element in an array with another variable. The other variable is coming from a session. So i have something like this. $num1 = $_SESSION['num1']; and i have an array set with data from the text file.. Now i want to see if($array1[0] == $num1). When they are equal to eachother, I want to display "ok". So everything should work, and I know the two equal eachother, but it never says they are equal. When I set $num1 = 1; they say they are the same, but when i use the sessions it never says they are the same, but I know they are. Could someone help me figure this out? Thanks, Chris Quote Link to comment Share on other sites More sharing options...
tauchai83 Posted May 7, 2007 Share Posted May 7, 2007 hi, could you please post ALL your code that you had done? Quote Link to comment Share on other sites More sharing options...
CSmith1128 Posted May 7, 2007 Author Share Posted May 7, 2007 yea sorry.. here is the code i am using... $num1 = $_SESSION['num1']; $array1= array(); $array2= array(); $x = 0; $fileName = "file.txt"; $handle = fopen($fileName, 'r'); while (!feof($handle)) { $array1[$x] = fgets($handle); $array2[$x] = fgets($handle); $x++; } fclose($handle); if($num1 == $array1[0]) { echo "ok"; } //---------------------------------------------------------------------- //---------------------------------------------------------------------- i am trying to compare the two values, but it doesnt seem to work. the values: $num1 = 1 and $array1[0] = 1. Quote Link to comment Share on other sites More sharing options...
Lumio Posted May 7, 2007 Share Posted May 7, 2007 try this here: <?php $filename = 'file.txt'; $file = file($filename); if (intval($_SESSION['num1']) == intval($file[0])) echo 'ok'; ?> Quote Link to comment Share on other sites More sharing options...
chronister Posted May 7, 2007 Share Posted May 7, 2007 where are you using session_start() at? You need this to initiate the session to begin with. Quote Link to comment Share on other sites More sharing options...
CSmith1128 Posted May 7, 2007 Author Share Posted May 7, 2007 yea i did session_start() at the top. intval() worked.. thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.