【问题标题】:php echo file_get_contents from a01.txt to a36.tx [duplicate]php echo file_get_contents 从 a01.txt 到 a36.tx [重复]
【发布时间】:2017-12-03 14:54:31
【问题描述】:

我正在调用要渲染的 36 个文件的内容。该脚本运行良好,但我想知道什么是更短的方法。我的代码:

<?php $arrays = file_get_contents('a01.txt'); echo $arrays;?> <?php $arrays = file_get_contents('a02.txt'); echo $arrays;?> <?php $arrays = file_get_contents('a03.txt'); echo $arrays;?> <?php $arrays = file_get_contents('a04.txt'); echo $arrays;?>

... X 36 直到 ('a36.txt')

类似

&lt;?php $arrays = file_get_contents('a04.txt-a36.txt'); echo $arrays;?&gt;

【问题讨论】:

  • 带有foreach 循环

标签: php


【解决方案1】:

使用循环

<?php 
for($i = 1; $i<=36; $i++) {
    // Add a zero before single digits
    $filename = 'a' . str_pad($i, 2, '0', STR_PAD_LEFT) . '.txt';
    $content = file_get_contents($filename);  
    echo $content;
}
?>

http://php.net/manual/en/control-structures.for.php

http://php.net/manual/en/function.str-pad.php

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2013-06-12
    • 2014-09-25
    • 2013-05-08
    相关资源
    最近更新 更多