hi all;

I have a recursive function the read the file, but its doesn work properly so It a big problem for me as a newbie in php, i need help to someone, here is my function that read the file and returning the line of file as array of string.

function processFile($filename) {
	
       $lines = file($filename);
       $returnArray= array();
       		foreach ($lines as $line) {
         	$output=processFile($string);
            if (gettype($output)=="array") {
               $returnArray[]='<tr><td>Title:</td><td>'.($output[0]).'</td><td>Value:</td><td>'.$output[1].'</td></tr>';
            }      
         }
         return $returnArray;
      }

I have another page to test the my function if it running but this is the way i got the error.

require_once('chadlib.php');
      $string = file('test.php');
		$strings=processFile($string);
         echo '<table>';
            foreach ($strings as $element => $value) {
         echo $value;
         echo '</table>';
		 }

please check theline of my code. thank in advance.

Dani AI

Generated

Short summary of the actual problems in the original posts: you passed an array where a filename (string) was expected, you call processFile($string) inside the function while $string is undefined in that scope, the table closing tag is echoed inside the loop, and gettype(...) == "array" is a fragile way to test for an array. was correct that file() returns an array of lines; fix the type mismatch or change the function to accept either a filename or an array.

A safer, clearer implementation pattern — keep data parsing separate from HTML rendering and make the function accept both a filename or an array of lines:

function process_file_source($source) {
    if (is_array($source)) {
        $lines = $source;
    } elseif (is_string($source) && file_exists($source)) {
        $lines = file($source, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    } else {
        trigger_error('Expecting filename or array of lines', E_USER_WARNING);
        return [];
    }

    $items = [];
    foreach ($lines as $ln) {
        $ln = trim($ln);
        if ($ln === '') continue;
        $parts = array_map('trim', explode(':', $ln, 2));
        if (count($parts) === 2) {
            $items[] = ['title' => $parts[0], 'value' => $parts[1]];
        } else {
            $items[] = ['line' => $ln];
        }
    }
    return $items;
}

Example rendering (note: close the table after the loop and escape output):

$rows = process_file_source('test.php');
echo "<table>\n";
foreach ($rows as $r) {
    if (isset($r['title'])) {
        echo '<tr><td>Title:</td><td>' . htmlspecialchars($r['title']) . '</td>'
           . '<td>Value:</td><td>' . htmlspecialchars($r['value']) . '</td></tr>' . "\n";
    } else {
        echo '<tr><td colspan="4">' . htmlspecialchars($r['line']) . '</td></tr>' . "\n";
    }
}
echo "</table>\n";

Troubleshooting tips: enable errors (error_reporting(E_ALL)), inspect variables with var_dump()/print_r() to confirm types, use is_array() instead of gettype(...)=="array", and decide whether the function should accept filenames (strings) or arrays — don't mix the two without explicit checks. This addresses the undefined-variable and type-mismatch issues raised by and .

Recommended Answers

All 7 Replies

Didn't you post this question already ? Don't double post. Anyway, here is the explanation again.

$string = file('test.php');

file() returns an array.

$strings=processFile($string);

You are passing an array as a parameter to the function processFile.

$lines = file($filename);

In function processFile, you are again trying to open an 'array' (instead of a file). Pass filename as a parameter for processFile function.

yes, but i was wondering maybe nobdy get my poin so im explaning in anohter way to understand, sory for that, I have alredy tried your replied but it doesnt work, it showing an erro desame with i got, thanks for replied again, :icon_confused:

What exactly are you trying to do with these 2 lines ?

$string = file('test.php');
$strings=processFile($string);

What exactly are you trying to do with these 2 lines ?

$string = file('test.php'); // $string is assign for the line of file. and

$strings=processFile($string); // this will get the line of string to return as an arry of string.

hope it now clear to you.

$string = file('test.php'); // $string is assign for the line of file.

That is where you are going wrong. If you want a string use file_get_contents. file() returns an array.

That is where you are going wrong. If you want a string use file_get_contents. file() returns an array.

ow; well that is another part for me to find out thanks, by the way, but can you give an exaple on how would i use the get_file_content to read the line of file?

Its in the link I have provided. $string = file_get_contents("test.php"); will read the entire file 'test.php' and returns it as a string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.