问题
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