• On Wednesday, 19th February between 10:00 and 11:00 UTC, the forum will go down for maintenance. Read More

Need help with stupid PHP include issue please?

Entrepreneur

BANNED
Joined
Oct 12, 2007
Messages
439
Reaction score
391
What it is, is i have a large site built in php and i'm converting it to a MySQL based CMS, to enable updating and moving it easier.

The path to my includes is specified in all my files, but while buidling a config file i decided, as i'm moving server, to add the path to an include within the config, then within all my php files access this path within that include. Therefore if the server changes again in the future, i edit 1 line in 1 file and not a shed load in many files.

What i did was create my config.inc.php file as below:

Code:
<?php
define('INCLUDE_PATH' , '/server/path/to/includes/');
?>

Then in all my files i include the above file first, and have the file itself within the public directory so doesn't require a path itself,

Code:
<?php
require_once 'config.inc.php';
?>

and then for any includes within say, the aboutus.php i use the include path from the config file as below.

Code:
<?php
include INCLUDE_PATH . "header.inc.php";
?>

This actually works perfectly, but it suddenly dawned upon me that, and this is probably because i'm crap at php, that when php does what it does, it would appear as below.

Code:
<?php
include /server/path/to/includes/header.inc.php;
?>

This obviously has no quotes or anything around it?

Is this an issue, should i be doing this in a different way, because although it works on my home computer/server test bed thing (running Apache, PHP and MySQL) i worry that this is incorrect and the tolerant language that is php is letting it slide?

As always, i go on, but help or a knowledgable answer would please me massively. Thanks. :)
 
Cool thanks. Out of curiosity, if you don't mind answering, why can it function ok without quotes when it's created in that manner? Because obviously under normal circumstances if i was to include the path without quotes it wouldn't function would it?
 
Cool thanks. Out of curiosity, if you don't mind answering, why can it function ok without quotes when it's created in that manner? Because obviously under normal circumstances if i was to include the path without quotes it wouldn't function would it?

Due to earlier design decisions php is to some extent loosely typed. For example
1. function names not case sensitive
2. Your can do some things in slightly different ways but still be correct
Code:
include ( 'file.php' );
include 'file.php';
include file.'.php';

It was a bad decision or overlook that breeds unnecessary differences in coding styles.
 
Personally I prefer 'require' statement instead 'include'; it helps me to write a more robust code, because if the file specified in the require statement is not found it causes a fatal error, include causes a warning message only. In this manner I have to focus my attention to avoid the file will not be found
 
It will depend on many things. Sometimes the file is not critic. In that case using include is fine. For critic things we should see if the file exists before the include/require.


Personally I prefer 'require' statement instead 'include'; it helps me to write a more robust code, because if the file specified in the require statement is not found it causes a fatal error, include causes a warning message only. In this manner I have to focus my attention to avoid the file will not be found
 
Back
Top