问题描述:

I've got simple PHP script to read TXT file and show content on html site:

<?php include('temperature.txt');

?>

Can anyone tell me how can I write simple code to read this TXT file and if Temperature = 99, show on site: OFF ?

File temperature.txt contains a temperature (for example number 25), but on start it's set on 99 which means everything is off (Arduino project). Is it possible to read temperature and if number is 99 show on html site "OFF" and if it's other than 99 show exact number?

Thanks for your support!

网友答案:

Maybe something like this?

<?php

$temperature = (float) file_get_contents('temperature.txt');

if ($temperature == 99) {

    echo 'OFF';

} else {

    echo $temperature;

}
网友答案:

Use file() to read the text file into an array and then get the first line, check if it's equal to 98, and echo the result accordingly:

$lines = file('temperature.txt');
$temperature = (int) $lines[0];

if($line == 98) {
    echo 'OFF';
}
else {
    echo $temperature;
}
网友答案:

Try this code

    <?php
        $lines = file("temperature.txt");
        if(strpos($lines[0], "=") !== false){
            $temp = explode("=",$lines[0]);
            $temperature = trim($temp[1]);
        }else{
            $temperature = (int)(trim($lines[0]));
        }

        if($temperature != 99){
            echo "Temperature is $temperature";
        }else{
            echo "OFF";
        }
    ?>

These are examples of temperature.txt

Temperature = 99

95

* = 95

(* you can write anything you want, because important char is '=')

相关阅读:
Top