file_get_contents从2个文本文件中添加数字 [英] file_get_contents Adding numbers from 2 text files

查看:89
本文介绍了file_get_contents从2个文本文件中添加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用cronjob将数字从API缓存到文本中,我有两个文本文件,其中包含数字,我想将两者像16 + 4一样加在一起,并以20的价格打印

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.

推荐答案

像这样:

<?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";
}

这篇关于file_get_contents从2个文本文件中添加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆