【问题标题】:file_get_contents Adding numbers from 2 text filesfile_get_contents 从 2 个文本文件中添加数字
【发布时间】:2014-04-03 03:24:27
【问题描述】:

我目前正在使用 cronjob 将数字从 API 缓存到文本中,我有两个带有数字的文本文件,我想像 16+4 一样将两者相加,并在 20 处打印

我不知道该怎么做

【问题讨论】:

  • 呃,你是说这样吗? echo trim(file_get_contents('file1.txt')) + trim(file_get_contents('file2.txt'));我偷偷怀疑你对我们来说太愚蠢了
  • 很棒的 "one-liner" @CrayonViolent +1
  • Crayon 起初我错过了你的评论,但它也很完美,而且它的代码更小。基本上我有一个从 API 运行的比赛,他们搞砸了一些事情,所以要组建第二个团队来运行并希望在不搞砸的情况下添加那个人。

标签: php file-get-contents addition


【解决方案1】:

像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多