r/PHPhelp • • Jul 26 '25

Basic php Help, Include Error, Pathing Issue?

Started coding in php , but I can't seem to use "include" or "require_once", really basic level stuff, syntax is correct because if I drop the file directly into the code it runs without issue, but if I try to pull a saved file I run into an error

code to call -

<?php include 'includes/footer1.php';?>

code being called -

<footer> &copy; <?php echo date('y')?></footer>
</body>
</html>

Warning: include(includes/footer1.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\New folder\class_objects.php on line 46

Warning: include(): Failed opening 'includes/footer1.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\New folder\class_objects.php on line 46

both files are saved in my root folder, so what am I doing wrong here.

3 Upvotes

38 comments sorted by

View all comments

-5

u/[deleted] Jul 26 '25 edited Jul 26 '25

includes is a reserved word and shouldn't be used.

however if you have an includes sub directory in the www directory you do:

include './includes/footer1.php';

But the php file you are including is not correct, as it should be all php and not a mix of html and php

so inside footer1.php it should look like this:

<?php
$footer= '<footer> &copy; '.date('y').'</footer></body></html>';

Then on the page where you want it:

echo $footer;

Because include does not load where you call include on the page. in a pure php page.

If you call it after ?> in the after HTML instance, it will be there where you put it. However, Its not an efficient way to use PHP

5

u/colshrapnel Jul 26 '25 edited Jul 26 '25

I am afraid you are mistaken, on all accounts

includes is a reserved word and shouldn't be used.

Not sure what you mean, but "includes" is not a reserved word and it can be used anywhere. Also, where exactly do you see it gets [mis]used?

you do include './includes/footer1.php';

it's no different from include 'includes/footer1.php'

But the php file you are including is not correct, as it should be all php and not a mix of html and php

Dunno where did you get it but this statement is outright wrong. included file can be a mix of php and HTML all right. So you don't have to write HTML in PHP strings.

Because include does not load where you call include on the page.

Not sure what you mean, but when "you call incude on the page", it loads all right, and executes PHP in that file

Its not an efficient way to use PHP

Again, not sure where did you get it, but this statement is also wrong.

1

u/Dr_philociraptor Jul 26 '25

I do have a folder inside of my root document saved as "includes", figured it would be a good place to set any files that would be used regularly.

1

u/colshrapnel Jul 26 '25

That's all right. Just always use absolute paths.

-5

u/[deleted] Jul 26 '25 edited Jul 26 '25

Not sure what you mean, but "includes" is not a reserved word and it can be used anywhere.

xampp uses it as an environmental. Even though I wouldn't waste my time with xampp

it's no different from include 'includes/footer1.php'

./ is shorthand for current directory path. But of course you must not be a Linux user because its a carry over from that system.

Dunno where did you get it but this statement is outright wrong. included file can be a mix of php and HTML all right. So you don't have to write HTML in PHP strings

order of execution. since include or required is a php command its output is php first. so another way if you just wanted to execute the file on the fly you would write the footer file like:

<?php
echo '<footer> &copy; '.date('y').'</footer>';
echo '</body>';
echo '</html>';

But calling php after html is a performance hit as the code wants to execute all php first then render html.

In older versions, if you use php after html, you can inject php code after it has been received to the client where ever they called <?php again in the html.

3

u/colshrapnel Jul 26 '25

xampp uses it as an environmental

So what? This code doesn't use "includes" as anything "environmental" (whatever it means). It just uses it as a directory name.

./ is shorthand for current directory path.

So just a directory name is. Go to your Linux console and type

cd /home
ls $USER
ls ./$USER

you will see same output.

order of execution. since include or required is a php command its output is php first.

PHP outputs included file exactly as it's written, your ideas are wrong. Write some test code and see.

-4

u/[deleted] Jul 26 '25

PHP outputs included file exactly as it's written, your ideas are wrong. Write some test code and see.

I find it bad practice to use php after html. As it was intended to run before any html and output html all at once.

So it has to render again if called afterwards in html.

Of course you could close php run the html but then its triple rendering.

<?php
?>
<footer> &copy; <?php date('y');?></footer>
</body>
</html>
<?php

Which is sloppy code that will render slowly.

Php is not a bad language. However, some are taught sloppy coding practices that end up with slow rendering. But since this under xampp it has its own set of rules and conditions that are not 100% compatible with a bare metal install on a linux server.

4

u/colshrapnel Jul 26 '25

I find it bad practice

Unfortunately, nobody else agrees with you. I would suggest to freshen your PHP knowledge.

-1

u/[deleted] Jul 26 '25

I find the 'nobody else' to be no one but you. As I haven't seen anyone chime in yet. And there is only 6 people who has looked at this and they seem to not want to nor care to chime in.

6

u/MateusAzevedo Jul 26 '25

You really should get back and relearn the basics. Everything you said in your comments is wrong.

2

u/mtetrode Jul 26 '25

I will chime in

Your advice is wrong, in all paragraphs there is at least one error.

You do not help the OP, only tell him that this is wrong and that is not good.

Why?

OP is someone that is starting to learn to program in PHP. I will not tell him to use Symfony components as Laravel uses too much behind the scenes magic, or that Postgres is purer than MariaDB.

I would - if it wasn't 0:30 in my timezone, write a helpful post with just enough examples to get the OP started

/rant

→ More replies (3)

2

u/equilni Jul 26 '25 edited Jul 26 '25

I chimed in, but please post on r/php your best practices so we can review. Perhaps we are all doing it incorrectly.

Please, o please, help us be better developers.

1

u/sneakpeekbot Jul 26 '25

Here's a sneak peek of /r/PHP using the top posts of the year!

#1: Anyone else still rolling this way? | 219 comments
#2: PHP is a hidden gem!
#3: PHP is Still the King!


I'm a bot, beep boop | Downvote to remove | Contact | Info | Opt-out | GitHub

0

u/[deleted] Jul 27 '25

I guess its better than what wordpress came up with: using functions to call header and footer, etc. Because executing functions is really slow compared to include.

Why didn't they just expand append and pre-append node like in php-xml? But with html?

→ More replies (1)

1

u/[deleted] Jul 27 '25

Hey man, I am not looking to pile on you, but you were in fact wrong in about every statement you made, although I do think you might be having some issues communicating what you meant. With that said, this thread was about the php include function not including a file. This is one of the reasons I use require_once, as a failed include should never be ok.

Colshrapnel, Mateus and others have been working primarily with PHP for a long time, and routinely provide help and advice to others.

At this point, my advice would be to relax and stop taking things personally. Be open to questioning your assumptions. Sometimes you find out something you believe to be true is not.

2

u/colshrapnel Jul 26 '25

Create 2 files. 1.php

Main file<br>
<?php
echo "Include goes below<br>\n";
include '2.php';

2.php

<b>HTML first</b><br>
<?= 'Then PHP' ?><br>
<b>Then HTML again</b><br>
<?= 'Then PHP again' ?><br>

then run 1.php and see

1

u/[deleted] Jul 26 '25

I understand the php code above.

to put that into standard form:

<b>HTML first</b><br>
<?php echo 'Then PHP';  ?><br>
<b>Then HTML again</b><br>
<?php echo 'Then PHP again'; ?><br><b>HTML first</b><br>
<?php echo 'Then PHP'; ?><br>
<b>Then HTML again</b><br>
<?php echo 'Then PHP again'; ?><br>

The short hand echo command

<?= equals <?php echo $variable or 'string' and ?> assumes EOL ; afterwards and close php inline.

1

u/Dr_philociraptor Jul 26 '25

oooookkkaaayyy now that solved the issue I was having

1

u/colshrapnel Jul 26 '25

Not sure what you mean

1

u/colshrapnel Jul 28 '25

Don't tell me you moved includes into New folder, did you?

1

u/Dr_philociraptor Jul 30 '25

I made a new floder and labled it as includes inside of the root documents folder

2

u/[deleted] Jul 26 '25

You can use "includes" in Xampp for filenames/paths like this. Xampp"s env has nothing to do with how Apache/PHP behave in this context. 

Your point about executing PHP after HTML is nonsense. All PHP is always executed before HTML is even considered. 

I don't know who's teaching you the points you've made, but fire them. 

2

u/equilni Jul 26 '25

includes is a reserved word and shouldn't be used.

In OP's context they can use it.

https://www.php.net/manual/en/function.include.php

https://www.php.net/manual/en/reserved.keywords.php

The following words cannot be used as constants, class names, or function names.

Is OP using include in this manner?

But the php file you are including is not correct, as it should be all php and not a mix of html and php

What? This is incorrect.

Second example here - https://www.php.net/manual/en/tutorial.firstpage.php

However, Its not an efficient way to use PHP

How to tell us you don't use templating without telling us you don't use templating....

https://phptherightway.com/#templating

-1

u/[deleted] Jul 26 '25 edited Jul 26 '25

see whats outputs when executing getenv(includes) on a xampp server. Because of the error message. and its is not a php error message.

I don't use templating that way if I ever use it.

A lot of ways PHP is taught wrong and even leave out standard practices like input sanitation from html fourms in php books.

1

u/equilni Jul 26 '25 edited Jul 26 '25

see whats outputs when executing getenv(includes) on a xampp server.

I don't use XAMPP, sorry. But I did what you ask.

https://onlinephp.io?c=%3C%3Fphp%0D%0A%0D%0Agetenv%28includes%29%3B&v=8.2.20

Fatal error: Uncaught Error: Undefined constant "includes"

Maybe you mean include?

https://onlinephp.io?s=s7EvyCjg5eLlSk8tSc0r08jMS84pTUnVtAYA&v=8.2.20

Parse error: syntax error, unexpected token ")", expecting ":"

Maybe I am doing something wrong. I am a self taught PHP dev from the end of PHP 4 days and led by so many bad practices early on by tutorials, other devs and the like.....

A lot of ways PHP is taught wrong and even leave out standard practices like input sanitation from html fourms in php books.

We're supposed to sanitize the data and not validate it? Wait, really???