2014-03-02 27 views
-1

我目前正在使用cronjob将数字缓存到API中的文本中,我有两个文本文件,其中包含数字,我想将它们加在一起,就像16 + 4一样, at 20file_get_contents从2个文本文件中添加数字

我不知道我该怎么做,如果有人可以提供一个例子,如果可以完成,我会非常高兴。

+3

嗯,你的意思是这样的吗? 'echo trim(file_get_contents('file1.txt'))+ trim(file_get_contents('file2.txt'));'我有一个偷偷摸摸的嫌疑,你对我们的贬低太多 –

+0

伟大的**“单行” ** @CrayonViolent +1 –

+0

Crayon我最初错过了你的评论,但它的作品也很完美,它的代码也很小。基本上我有一场从API开始的竞赛,他们搞砸了一些东西,所以让第二个团队运行,并希望添加这个人而不会搞砸一些东西。 – Gav

回答

0

像这样:

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

这工作完美!比我预想的要好。 – Gav

相关问题