PHP: how to create a file and write text? I want to encapsulate a method to create a file and write two width and height parameters to the text.
Effect after implementation
For example, 100 300
The generated file name is s 100300. less.
The content is
@ Import "shiying ";
@ Screen_width: 100px;
@ Screen_height: 300px;
How to write this function .. Red must match
Reply to discussion (solution)
function mkless($width, $height) { $r[] = '@import "shiying";'; $r[] = sprintf('@screen_width:%spx;', $width); $r[] = sprintf('@screen_height:$spx;', $height); file_put_contents("s$width$height.less", join(PHP_EOL, $r));}
function mkless($width, $height) { $r[] = '@import "shiying";'; $r[] = sprintf('@screen_width:%spx;', $width); $r[] = sprintf('@screen_height:$spx;', $height); file_put_contents("s$width$height.less", join(PHP_EOL, $r);}
Parse error: syntax error, unexpected ';' in D: \ wamp \ www \ ceshi \ xieru. php on line 14
An error is reported, and the fileputcontent cannot generate files automatically ..
File_put_contents ("s $ width $ height. less", join (PHP_EOL, $ r );
Parentheses
The file_put_contents () function writes a string to a file.
It is the same as calling the fopen (), fwrite (), and fclose () functions in turn.
Syntax
File_put_contents (file, data, mode, context)
Parameter description
File is required. Specifies the file to write data. If the file does not exist, create a new file.
....
...
Is a function call problem.
File_put_contents
By the way, the code
$filename='s100300.less';$date='@import "shiying";@screen_width:100px;@screen_height:300px;';file_put_contents($filename,$date);