Page 1 of 1

I don't see the error with this array

Posted: Wed Jul 21, 2021 2:35 pm
by KenHorse

Code: Select all

	$handle = fopen("/etc/rpt.fifo", "r") or die("Unable to open file!");
                $buffer = trim(fgets($handle));				
                $ary = explode (",", $buffer);  
		$x = $ary[2];
		print $x;
		flush();   
		pclose($handle);

		
When I run it, it reports:

Notice: Undefined offset: 2 on line 4

This is the line in /etc/rpt.fifo that is being read (there are 4 indices)

20210721153256,1100,TXKEY,89790

The error makes no sense to me but I'm no expert <g>

Re: I don't see the error with this array

Posted: Thu Jul 22, 2021 12:59 am
by simonbrahan
Debugging 101! You need to look at what your code is dealing with on each line. You can do this with echo, print_r, var_dump, any outputting function.

Code: Select all

$handle = fopen("/etc/rpt.fifo", "r") or die("Unable to open file!");
var_dump($handle); // should give you something like "resource(2) of type (stream)". If it doesn't your attempt to open the file has failed.
$buffer = trim(fgets($handle));
var_dump($buffer); // should give you something like "20210721153256,1100,TXKEY,89790". If it doesn't you're not reading what you think you are.
$ary = explode (",", $buffer);
var_dump($ary); // should give you something like "array("20210721153256","1100","TXKEY","89790")". If it doesn't, maybe your comma characters are badly encoded.
$x = $ary[2];
print $x;
flush();   
pclose($handle);