file_get_contents Adding numbers from 2 text files

只愿长相守 提交于 2019-12-23 07:04:50

问题


I'm currently using a cronjob to cache a number into text from an API, I have two text files with numbers in them and I want to add both together like 16+4 and it prints at 20

I have no Idea how I would do this and would be really glad if someone could provide an example if It can be done.


回答1:


Like this :

<?php

$file1 = '/path/to/file/file1.txt';
$file2 = '/path/to/file/file2.txt';

if(is_file($file1) && is_file($file2)) {

    $value1 = trim(file_get_contents($file1));
    $value2 = trim(file_get_contents($file2));

    echo sprintf('The sum is %s', (int)$value1 + (int)$value2);

} else {
    echo "One of the files doesn't exist";
}


来源:https://stackoverflow.com/questions/22123737/file-get-contents-adding-numbers-from-2-text-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!