SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    baffling encounters with "require" and "define" syntax

    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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    <snip>
    Last edited by Force Flow; Sep 1, 2013 at 11:09. Reason: Removed unhelpful comment

  3. #3
    SitePoint Enthusiast Belsnickle's Avatar
    Join Date
    Feb 2012
    Location
    Chico, CA
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,003
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    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.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Though not technically correct, I think of CONSTANTs as "non-changing variables" (once they are DEFINEd).
    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.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •