Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Include function not working [closed]

I'm trying to develop a website. I want that different sections of site that will be written in PHP will be in different file. Such as "posts.php", "recents.php" etc. I'm trying to use the function "include" to include the files from the directory "/codes/". But it is not working.

The thing happen is, if I put <?php include '/codes/posts.php' ?> in the middle of the original HTML page (the page is saved in PHP format). The code portion before the line added is working, but the portion after the line added is vanished. Don't know why. What am I doing wrong?

like image 288
Espero Shoumma Avatar asked Feb 13 '26 05:02

Espero Shoumma


2 Answers

After fixing the semi-colon issue, also consider that:

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

form the PHP docs

so either include './codes/posts.php'; or include 'codes/posts.php', the first version looks for the file, relative to the current working directory, the second uses the include path.

PS: best use require_once or include_once, to avoid including the same function/class definitions multiple times, which will casuse issues (redeclaring functions)

like image 116
Elias Van Ootegem Avatar answered Feb 15 '26 18:02

Elias Van Ootegem


Try this one, you forgot to add a single quote :

<?php
// Enable error logging: 
error_reporting(E_ALL ^ E_NOTICE);

// Add a single quote: 
include '/codes/posts.php';
?>
like image 22
Duikboot Avatar answered Feb 15 '26 17:02

Duikboot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!