Maison  >  Article  >  développement back-end  >  PHP怎么创建文件并且写入文字

PHP怎么创建文件并且写入文字

WBOY
WBOYoriginal
2016-06-23 13:50:341138parcourir

我要封装个方法 创建文件并且写入文字 参数2个 width和height

实行后的效果     
比方说如100 300

生成文件名叫s 100300.less

里面内容是
@import "shiying";
@screen_width: 100px;
@screen_height: 300px;


这个函数怎么写。。 红色的要对应哦


回复讨论(解决方案)

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
报错了 而且这个fileputcontent不能自动生成文件吧。。

 file_put_contents("s$width$height.less", join(PHP_EOL, $r);
少个括号

file_put_contents() 函数把一个字符串写入文件中。

与依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
语法

file_put_contents(file,data,mode,context)

参数  描述
file  必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。
....
...

就是个函数调用 的问题

file_put_contents

顺便符上代码

$filename='s100300.less';$date='@import "shiying";@screen_width:100px;@screen_height:300px;';file_put_contents($filename,$date);

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn