Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating JS Files using php

I have a code like this which is created using php

<?php
$script="$(document).ready(function(){alert('hai');});";
?>

As far as I have referred in other websites, they have used fopen() and fwrite() to open a text file and write in it. I don't want txt file to be created.Can I create a script file say script.js that contains data in the $script variable using php?

like image 266
Ganesh Babu Avatar asked Feb 15 '23 10:02

Ganesh Babu


1 Answers

This will create file called script.js and contains your JS statements:

$script="$(document).ready(function(){alert('hai');});";
$fileName="script.js";
file_put_contents($fileName, $script);
like image 133
Charaf JRA Avatar answered Feb 24 '23 00:02

Charaf JRA