Search code examples
phploopsfile-get-contents

Echo file_get_contents in a loop


I have some files in core directory. Example of file names: 1 2 3 4 5 6 7 8 9 10 ... I want to echo them as (loop):

content of file 1

content of file 2

content of file 3

content of file 4

content of file 5

content of file 6

............

............

I tried the following code, but it is not working. Please help!

<?php
for ($i=1; $i<=10; $i++)
{
$x=file_get_contents('core/$i');
echo $x . '<br>';
}
?>

Solution

  • You are setting $i as type text by mentioning it in ''

    Change your code to

    <?php
    for ($i=1; $i<=10; $i++)
    {
    $x=file_get_contents('core/'.$i);
    echo $x . '<br>';
    }
    ?>