In: Computer Science
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
PHP File :
<!DOCTYPE html>
<html lang="en">
<head>
<!-- title for web page -->
<title>PHP random Numbers</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php
//creating a file
$file=fopen("numbers.txt","w");
//using for loop
for($i=0;$i<10;$i++)
{
//generate random number in PHP
$randomNumber=rand(1,100);
echo $randomNumber."<br/>";//display number in the browser
//write number to the file
fwrite($file,$randomNumber.PHP_EOL);
}
//close the file
fclose($file);
?>
</body>
</html>
======================================
Screen showing numbers in the browser :
Screen showing numbers in the numbers.txt file: