Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DEFINE vs Variable in PHP

Tags:

php

Can someone explain the difference between using

define('SOMETHING', true); 

and

$SOMETHING = true; 

And maybe the benefits between one or the other?

I use variables everywhere and even in a config type file that is included to everypage I still use variables as I don't see why to use the define method.

like image 636
JasonDavis Avatar asked Aug 03 '09 23:08

JasonDavis


1 Answers

DEFINE makes a constant, and constants are global and can be used anywhere. They also cannot be redefined, which variables can be.

I normally use DEFINE for Configs because no one can mess with it after the fact, and I can check it anywhere without global-ling, making for easier checks.

like image 164
Tyler Carter Avatar answered Oct 11 '22 12:10

Tyler Carter