Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - What are constants, are they good practice, and how do they differ from variables?

Tags:

php

Beginner question...

How different is define("$a",365); from $a = 365;?

Thanks!

JDelage

like image 577
JDelage Avatar asked Sep 06 '25 21:09

JDelage


1 Answers

If you define constant define("YEAR",365); you can't change it during execution time so YEAR will always be 365 no meter what. On the other hand variables can change their value during script execution also they have local scope, which means they are available just in the function file they are declared. Constants have global scope they can be accessed from all over the script.

http://php.net/manual/en/language.constants.php

http://planetozh.com/blog/2006/06/php-variables-vs-constants/

like image 177
Alex Rashkov Avatar answered Sep 08 '25 11:09

Alex Rashkov