Search code examples
phpmathtext-filesequation

How to add number text files into a PHP math equation?


This is part of script i am putting together earlier. This time I am trying to pull the numbers in the equation from 3 different txt files. Each text file only contains 1 number with no other lines. Example:

35
21
3

Those are the numbers However, I don't know how to write the php code to call for the txt files to be used in the code. I just put an idea of what I am trying to do below. I hope it makes some sense

CODE:

 <?php
$filename = 'https://www.justtestdomain.com/results.txt';
$filename = 'https://www.justtestdomain.com/results2.txt';
$filename = 'https://www.justtestdomain.com/results3.txt';
$data = file($filename);
$x= $results;
$y= $results2;
$w= $results3;  

$z=$x-$y-$w;  
echo '<script>var z = ' . $z . ';</script>';  
?> 

I tried to something similar with JavaScript but it wasn't what I needed. Anyway, is what I am doing possible? Anyway, thanks for your help in advance


Solution

  • While this is not a very good way of getting data to a javascript, this should work.

    $filename   = 'https://www.justtestdomain.com/results.txt';
    $data       = file($filename);
    $x          = $data[0];
    
    $filename = 'https://www.justtestdomain.com/results2.txt';
    $data       = file($filename);
    $y          = $data[0];
    
    $filename = 'https://www.justtestdomain.com/results3.txt';
    $data       = file($filename);
    $z          = $data[0];
    
    $A = $x - $y - $z;
    echo '<script>var z = ' . $A . ';</script>';