Re: define() or $variable for application settings?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 07:56 PM 2/8/2006, Matt Arnilo S. Baluyos (Mailing Lists) wrote:
I have a config.inc.php file which basically contains all the
configuration info that the applications needs (directory/file
locations, database credentials, etc). The information there is set
using the define() function.

However, I've seen some open-source projects which either use
$variables or associative arrays to save the values.


A small but significant factor in deciding whether to use define()d constants is that constants can't be integrated into expressions as neatly as can variables.

Variables can be embedded in expressions:

        $sUserName = "Froggy";
        echo "Variable: $sUsername.";

        Result:  Variable: Froggy.

Constants can't be embedded in expressions:

        define("sUserName", "Froggy");
        echo "Constant: sUsername.";

        Result:  Constant: sUsername.

In order to use constants in string expressions it's necessary to use concatenation operators:

        echo "Constant: " . sUsername . ".";


Ditto for heredocs:

Variables can be embedded in heredoc expressions:

        $sUserName = "Froggy";
        echo <<< hdExample1
        Variable: $sUserName
hdExample1;

        Result:  Variable: Froggy.

Constants can't be embedded in heredoc expressions:

        define("sUserName", "Froggy");
        echo <<< hdExample2
        Constant: sUserName
hdExample2;

        Result:  Constant: sUsername.

Likewise, functions & classes can't be embedded in expressions without use of concatenation operators, and can't be used in heredocs.

Of course I'm not suggesting that our inability to embed non-variables in string & heredoc expressions removes them from the toolbox, it simply informs our choices within a given application. Personally I love using heredocs, functions, and classes and simply work around the embedding limitation.

Paul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux