Include() function is not working

Take a look at the image first

I have given the right path - include(‘includes/variables.php’); but it is showing an error

Warning: include(includes/variables.php): failed to open stream: No such file or directory:pensive:

As I see it index.php and variables.php are in the same folder? But this could just be the way the file tree is displayed in phpstorm.

Try include(‘/includes/variables.php’);

I don’t see close php tag
If you have close php then try

include ("/includes/variables.php");


include ("./includes/variables.php");

I don’t think you have, or you wouldn’t be getting that error!

include is not a function, it is part of the language. Whilst it works with brackets, the correct usage is:
include 'whatever.php';

Sorry guys, it is my fault that I had opened the wrong index.php file which is located in htdocs in xampp folder but the actual project index.php is located at Document folder of mac :banghead:

1 Like

Closing the PHP tag or not closing it wouldn’t matter. The only thing that could affect an include and require are

  • Typo in filename
  • File is located somewhere else
  • File doesn’t have the correct permissions (will result in permission denied)
  • File doesn’t exist

Generally, most PHP users don’t close it because there is no need to. PHP will not complain about this so most people don’t close it.

1 Like

Unless you are an engineer and do things correctly :sunglasses:

1 Like

I never close because have read about superfluous white space errors.

1 Like

So are you saying that all these aren’t done by professional “engineers” and not done correctly?

Symfony’s github (just an example)

PHPMailer’s github (just an example)

Larvel’s website (just an example - check home page for sample codes)

CakePHP’s github (just an example)

ircmaxell’s password_hash backwards compatibility (just an example)

So are you saying that these are all wrong even though they are top leading products in PHP?

The reason why most people including myself don’t end PHP tags is because based on varieties of IDE, you can actually get white spaces in your files. This can lead to headers already sent errors. And that’s actually a huge issue. Depending on how big your project is, it could take days to debug which file actually has the white spaces. Certain IDE will add in new line breaks after the last line in the file. gedit is one of those.

When I first started using Linux - Ubuntu, the only IDE I was introduced and knew about was gedit. It wasn’t good, but it got everything done and it remembers what files I opened. So I stuck with it. Not long did I find out that gedit was adding in new line breaks for no reason. IIRC, I tried asking a couple of websites a while back if they could make or find a plugin for gedit to remove the white spaces, but they said that gedit can’t be done that way. The white spaces were meant for something. I don’t recall. This was a few years back.

Not closing the PHP tag simply would avoid headers already sent because if the IDE creates a new line, the PHP tag isn’t closed so it’ll just act like a new line within PHP and not an actual new line for HTML.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.