Supposedly "define" determines a constant. Yet in lines 42 and 31 below the constant name refers to a folder.
How does this syntax work? What does it cause to happen within an application.
Supposedly the "require" statement identifies a file. But in line 33 the file "database_tables.php" is identified by two proceeding constant names. Similarly in line 52 the file "header.php" is identified by one proceeding constant name. Presumably in both cases the constants had been set to some value before they were prefixed to the file name. How does this syntax work?
Line #33 : require(DIR_FS_CATALOG . DIR_WS_INCLUDES . 'database_tables.php');
Line #42 : define('DIR_WS_INCLUDES', 'includes/');
Line #52 : require(DIR_WS_INCLUDES . 'header.php');
Line #31 : define('DIR_WS_CLASSES', DIR_WS_INCLUDES . 'classes/');
Thanks for taking the time to consider this. It's elementary to someone, but not myself.
The period in each of those examples is a concatenation symbol in php. That means the "contents" of the constant will be joined to the contents of the following string making one long string/value that is then used by the function/construct name.
define is setting your path and require is using that predefined constant to get the correct path to the file.
The reason it's doing it this way is so that the path is consistent through generic changes in elements of it, like say the server itself, and so that when that does change you only have to update one line of code instead of dozens or, in some cases, it can be determined by a stored variable containing server path information (like mediawiki, most forums software, and most CMSes do) which do it that way to stay modular.
Thanks greatly Belsnickle for your prompt reply. That was exactly the expansion of my text book discussions that I required. Of course -concerning ginerjm's statement- the concatenation symbol and the idea of concatenation were not the sources of confusion.
Hi PHPstumped, welcome to the forums.
To keep things on-topic:
Though not technically correct, I think of CONSTANTs as "non-changing variables" (once they are DEFINEd).
As Belsnickle mentioned, this is very common and a big time saver in terms of maintenance.
Actually, that is technically correct. If you try to define() something that was already defined, you get an error. So, it's a write-once, then read-only variable.
This topic is now closed. New replies are no longer allowed.