Is it possible to open these files in PHP, and merge these files to $file (into one variable)?

Viewed 44

I have a little code fragment:

if($sDate ==  "2022-07-18" || $sDate ==  "2022-07-19" || $sDate ==  "2022-07-20" || $sDate ==  "2022-07-21" || $sDate ==  "2022-07-22")
          {
            $file = fopen ("files/bla1.exp" , "r" );
            $file = fopen ("files/bla2.exp" , "r" );
            $file = fopen ("files/bla3.exp" , "r" );
          }

I want to open the three files and get all the content in $file. Is that possible. Can I use get_file_contents for that purpose?

I hope I explained my question well. Thanks in advance for answering my question!

1 Answers

Yes, you can use file_get_contents if you would like. It's also bit cleaner :)

Also you can append a string to a string with . operator.
In my example im using the short form of it:

$file = '';
if ($sDate ==  "2022-07-18" || $sDate ==  "2022-07-19" || $sDate ==  "2022-07-20" || $sDate ==  "2022-07-21" || $sDate ==  "2022-07-22")
      {
        $file .= file_get_contents("files/bla1.exp");
        $file .= file_get_contents("files/bla2.exp");
        $file .= file_get_contents("files/bla3.exp");
      }
Related