0

I have written a the beginning of a class and there is already a weird problem:

<?php
include('class-Park.php');
class park_searcher {```

    public function __construct() {
        // read json file and create each 'Park'
        $this->readjson();
    }

    public function print_parks() {
        // print every park as a json array that the javascript can use
    }
   
    // read json file and create each 'Park'
    public function readjson() {
        // read json file and create each 'Park' and add the complete object to an array of every park

        // why is readfile() printing the contents of the file?
        $raw = readfile("library/parks.txt");

    }

}

Instead of readfile() just setting $raw to the value of the file, the browser is echoing the contents of the file onto the page. Why is the readfile() function acting like echo and printing out the file contents?

David
  • 65
  • 2
  • 8
  • 1
    The manual for `readfile()` says *Reads a file and writes it to the output buffer.*! – Nigel Ren May 06 '21 at 15:33
  • `Why is the readfile() function acting like echo`... because that's its job. – ADyson May 06 '21 at 15:35
  • @NigelRen yes but the output buffer stays hidden unless the computer is told to print it to the page which there is no command to do. – David May 06 '21 at 15:36
  • @ADyson are you saying that I should use a different function to keep the file contents hidden? – David May 06 '21 at 15:36
  • 1
    `the output buffer stays hidden unless the computer is told to print it`... Where did you read that? Not sure you've got accurate info there. That isn't the default behaviour. Anyway to read the file into a string you need https://www.php.net/manual/en/function.file-get-contents.php. read the duplicate question in the blue box above your question - it already shows you what to do. – ADyson May 06 '21 at 15:37
  • 2
    [What is output buffering?](https://stackoverflow.com/questions/2832010/what-is-output-buffering) – Nigel Ren May 06 '21 at 15:37

0 Answers0