Skip to content
PHP Logo

What is the difference between including a file using include() and file_get_contents() in PHP?

The main difference between include() and file_get_contents() in PHP is that include() executes the included file as PHP code, while file_get_contents() simply reads the file contents as a string. If you want to include a file and execute its PHP code, you should use include(). If you just want to read the contents of a file as a string, you should use file_get_contents().

php
// Using include() to include and execute a PHP file
include('file.php');

// Using file_get_contents() to read the contents of a file as a string
$file_contents = file_get_contents('file.txt');