Creating and writing files with php

Is it possible to pass a variable to 'file_get_contents' in php? I get errors and wonder if this was my syntax. I am using the code below.

$page=file_get_contents('http://localhost/home/form.php?id={$data['form_id']}');
$fp=fopen('form.html','w+');
fputs($fp,$page);
fclose($fp);
+3
source share
2 answers

To use this syntax, use "quotation marks instead '.

$page=file_get_contents("http://localhost/home/form.php?id={$data['form_id']}");

or

$page=file_get_contents('http://localhost/home/form.php?id='.$data['form_id']);

+6
source

I prefer to use only one method for writing and reading a file, for example, this combination of 2:


Combination

Record: file_put_contents

Reading: file_get_contents


Combination of two

Record: fwrite

Reading: fread


In my opinion, this is a little more consistent.

+2
source

Source: https://habr.com/ru/post/1758601/


All Articles