Need Help

Have a look at this code:

<?php $x=define("test","Help me"); echo test; //This outputs the string "Help me" as usual echo $x; //This outputs "1" WHY ??? ?>

If anyone is seeing this kindly response on my email address(–removed–).
Thanks.

define - Defines a named constant

[php]example define(“USERNAME”, ‘Strider’);[/php]

So what you did is define a constant then assign the result to $x, in the case TRUE (1 = TRUE, 0 = FALSE).

Sorry I don’t email …

The results you displayed is correct. Perhaps you do not understand usage of assignments in PHP.

If you set a variable to the RESULTS of a function such as $x=define(); then it gives you a boolean that
says if it succeeded or not. So, you did not need the $X= part in defining an assignment to a constant!

Also, I seldom use “define”. I use SESSION variables since most define’s are used per user not global code
on the site. Not sure if that makes sense to you either.

And, yes, I agree with Strider, we have this site, no need for emails…

The only time I use define is for a database connection, for example:
[php]$this->_connection = new PDO(‘mysql:host=’ . DATABASE_HOST . ‘;dbname=’ . DATABASE_NAME . ‘;charset=utf8’, DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);[/php]

This way I don’t have memorize the connections, I can have it split between a local server and a remote server [php]if (filter_input(INPUT_SERVER, ‘SERVER_NAME’, FILTER_SANITIZE_URL) == “localhost”) {
define(‘DATABASE_HOST’, ‘localhost’);
define(‘DATABASE_NAME’, ‘’);
define(‘DATABASE_USERNAME’, ‘’);
define(‘DATABASE_PASSWORD’, ‘’);
define(‘DATABASE_TABLE’, ‘users’);
} else {
define(‘DATABASE_HOST’, ‘’);
define(‘DATABASE_NAME’, ‘’);
define(‘DATABASE_USERNAME’, ‘’);
define(‘DATABASE_PASSWORD’, ‘’);
define(‘DATABASE_TABLE’, ‘users’);
}[/php]

and I can show my code connection scripts without having to take out my security information. :wink:

Yes, I agree, Strider64… I do sometimes use a “DEBUG” define if I want to show myself items of interest.
But, most always, defines are not needed. Since I seldom show my own connections in posted code, I just
hard code it and show a sampler from w3schools or wherever…

Hmmm, perhaps you should drum up a tutorial on connection strings and show this code in that for others.

There are several instances where constants are needed. Anytime you are using a variable in several places. Instead of hardcoding the value, you would use a constant. That way, when you need to change that value, you only update it in one place and not have to track down everywhere you use that value.

File paths are another where it is common place to use. If you change servers, it is easier to update one line than to go through several and find where you used the path in a string.

Sponsor our Newsletter | Privacy Policy | Terms of Service