I don't see the error with this array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
KenHorse
php-forum Active User
php-forum Active User
Posts: 25
Joined: Sat Dec 26, 2015 12:18 pm

Wed Jul 21, 2021 2:35 pm

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>
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 140
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Thu Jul 22, 2021 12:59 am

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);
Post Reply