Search code examples
phpvariablesechofile-get-contents

How to retrieve all contents of a file from file_get_contents through PHP?


I am trying to work out how to retrieve all of the contents of a text file as a string and store it in a variable through PHP.

The code below works fine through file_get_contents and the PHP code is sent to the $get variable correctly, however, it doesn't get displayed on index.php correctly.

The index.php file will echo out hi rather than just hi.

index.php

<?php
    $display = "hello";
    $get = file_get_contents("get.txt");
    echo $get;
?>

get.txt

hi, $display;

How would I go about fixing this? I would really appreciate any help, thanks!


Solution

  • index.php:

    <?php
       echo 'hi';
    ?>
    

    some.php:

    <?php
        require 'index.php';
    ?>
    

    Will be shown text hi.