1

What is the difference and/or benefit of using define. for example in db file i can define password or any variable that i am going to use throughout website like this:

define('DB_PASSWORD', 'mypassword');

Instead of above code, I can also use it in db file like this:

$DB_PASSWORD="mypassword";

Both will serve same purpose... db file will be included in every page of website. So is there any benefit of using define rather then just declaring a variable.

3 Answers 3

6

Define defines a value, a constant which you can not change while variable means it's value may or will vary.

EDIT: Both have practical usage. There are sometimes data which should not be changed or it is always the same. For example a hex code of color blue will always be 0000FF or a database password may never be changed while the session is running.

It's more of a security tool than a performance or readability concern. Once you DEFINE, there's no way back, but you're sure your data is the same across the whole instance.

Sign up to request clarification or add additional context in comments.

Comments

3

define('DB_PASSWORD', 'mypassword'); cleated and defines a GLOBAL CONSTANT that cannot changed (well not easily) and when you call it you will not need the "$" sign.

Comments

2

For data that you do not intend to change throughout your script (static data such as a database password) you should define it. If you will ever re-assign it a new value, use a variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.